Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Writing a Python C extension in 2018
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Jean-Baptiste Aviat
May 16, 2018
Programming
1.2k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Writing a Python C extension in 2018
Jean-Baptiste Aviat
May 16, 2018
More Decks by Jean-Baptiste Aviat
See All by Jean-Baptiste Aviat
Content Security Policy in your Rails apps in 30s
aviat
0
330
App Security is just a Git flow (with built-in conflicts)
aviat
0
1.1k
Tune your App Perf (and get fit for summer)
aviat
2
550
Other Decks in Programming
See All in Programming
Webエンジニアなのにブラウザの仕組みがわからないので、Pythonで自作してみた
tatsuki12
4
1.1k
AIの中の人になってみる
htkym
0
140
Flow は今どうなっているか
mizdra
PRO
0
750
React本体のコードリーディング
high_g_engineer
1
160
Jindong: Introducing Declarative Haptics in Compose Multiplatform
l2hyunwoo
0
130
freeeにおけるEvalsの実践例の紹介
freee
PRO
0
160
言葉の格闘技のススメ~紙とペンと言葉から始める、キャリアの描き方~
progresscicada
2
180
わからない話を追いかけたら、プログラミング言語を作る側にいた
ydah
3
560
コンパウンドプロダクト開発のためのローカルプロセスマネージャー再発明 #layerxgo
izumin5210
0
400
FastAPI の並行処理モデルを完全に理解する
hoto17296
8
3.1k
片田舎のおっさん、 Swift Buildのダイアモンド問題解決の不具合修正PRを出すが、解決方法がキャッシュをしないようにすることであり、ビルド時間が伸びると言われてマージされないので高速化もする/swiftbuild
yimajo
0
310
AWS Transform Customによる Spring Boot 2.xから4.xへのVerUp
satoshi256kbyte
2
110
Featured
See All Featured
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.3k
The SEO Collaboration Effect
kristinabergwall1
1
530
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
2.5k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
950
It's Worth the Effort
3n
188
29k
Balancing Empowerment & Direction
lara
6
1.3k
The Limits of Empathy - UXLibs8
cassininazir
1
620
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
290
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
430
Building a Scalable Design System with Sketch
lauravandoore
463
34k
How Software Deployment tools have changed in the past 20 years
geshan
1
34k
Transcript
Writing a C Python extension in 2018 Paris.py 2018
Jean-Baptiste Aviat CTO & Co-founder of sqreen.io Former Hacker at
Apple (Red Team) PyMiniRacer author
[email protected]
@jbaviat Who Am I?
Someday… we needed to use V8 from Python. What we
ship: • is public • is widely used • need to have frictionless install $ pip install sqreen —> it just works™!
None
PyMiniRacer: cool JS binding github.com/sqreen/PyMiniRacer
The problem • V8 is C++ • How do you
run C++ in Python? …We need some kind of binding between these 2 worlds.
Many popular packages rely on binary cryptography numpy pymongo psycopg
simplejson lxml sqlalchemy…
People do it! Let’s do it too.
What are our goals? • minimize maintenance • make setup
easy • make testing easy • have great performance • have low memory fingerprint • dev time is a constraint We want to: And (obviously)…
built-in pythonic Python version independant open to other languages high
throughput capable CPython ✔ ✔ ✔ ctypes ✔ ✔ ✔ cffi ✔ ✔ ✔ ✔ Cython ✔ ✔ SWIG ✔ ✔
ctypes Built into Python Binary is Python independant: • can
be used on any Python version • can be used in other languages! No tight integration to Python • not high throughput capable • less Pythonic Complex syntax (C types wrapped in Python…) Not for C++
$ python >>> path = "./hello.so" >>> import ctypes >>>
lib = ctypes.cdll.LoadLibrary(path) >>> lib.hello_world() Hello world! C file Python interface binary object
Overview V8 (C++ interface) C interface to V8 Python interface
3rd party binaries import ctypes class PyMiniRacer(object): … #include <v8.h> int miniracer_init(); … V8 library (libv8.a) V8 headers (v8.h) linking ctypes C/C++ code Python library
How to put this together? $ cat setup.py from distutils.core
import setup, Extension extension = Extension('hello', ['hello.c']) setup(name=‘hello', version='1.0', ext_modules=[extension]) $ python setup.py build running build running build_ext building 'hello' extension clang […] -c hello.c -o hello.o creating build/lib.macosx-10.6-intel-2.7 clang -bundle […] hello.o -o hello.so
None
None
Ship it
Ever had trouble installing packages?
None
(at Sqreen, we are dog friendly) Come get your dog
sticker!
Python packaging history sdist (source distribution) eggs wheels —> manylinux
wheels (built distribution) 2004 2012 2016 Python 2.4 Python 3.3 Python 3.6 ❤
manylinux wheels Python standard: PEP513 / PEP571 Compatible on most
(real world) Linux Only in pip >= 8.1 Need build for all Python versions Binaries need to be built on CentOS 5
CentOS 5?? PEP571 → bumped requirement to CentOS 6!
Wheels vs compilation iso builds (crash can be reproduced) many
packages need maintenance one build per user only one package harder to install…
Best of 2 worlds We can have: ctypes: use a
Python independant binary wheels: ship compiled binaries By combining these… ctypes + wheels = ship only one binary
Thanks! Questions? We’re hiring! https://sqreen.io/jobs