Python-C integration: Ctypes, CFFI or create a Binary Module
Asked Answered
A

1

13

Basically I want to make a Python program call functions written in C.

So (as far as I know) my options are:

  • CTypes/CFFI
    • Create a DLL/SO/DyLib containing the C functions and access them using CTypes or CFFI. Apparently CFFI is way faster with the only drawback of having to declare in python all the functions signatures.
    • Pros:
      • Don't have to make any adaptation in my C functions. All type-translation is done in Python.
    • Cons:
      • Performance ?
  • Python Binary Module
    • Write a python interface in C, converting my C module into a binary python module
    • Pros:
      • Performance ?
    • Cons:
      • All type-translation is done in C. Using [SIP][3] this might be automated.

Convert the C module into a python binary module is really faster ?

Does both solutions support sending python callbacks to C functions ?

Is SIP a good option to generate a python interface ? Are there any other options ?

Are there any other particularities in any of them ?

Annuity answered 19/7, 2013 at 21:21 Comment(1)
Found some good stuff: 5 ways to use Python with native code, Python Wrapper Tools: A Performance StudyAnnuity
M
1

I was just reviewing an old list of options I published related to this: http://stromberg.dnsalias.org/~strombrg/speeding-python/

If you're only targeting CPython (2.x or 3.x), I'd probably go for Cython.

If you want to be able to run on Pypy too, CFFI might be good; I've not tried it yet, but it sounds great. It's not entirely like ctypes though - ctypes is more ABI level, while CFFI is more API level - which is nice.

If you want to be able to run on Jython too, subprocess is probably your best bet.

Mannish answered 19/7, 2013 at 21:32 Comment(5)
The C functions are already written. Tons of stable code. I'm only looking for a way to use then in python.Annuity
That's interesting, though my suggestions probably still apply.Mannish
Supposedly ctypes is slow at the python<->C boundary. They've optimized this for some common uses in Pypy.Mannish
I'm being told at pyconuk this week that using cffi is nowadays faster than using ctypes, especially for applications like OpenGL where you're constructing arrays of C types and then frequently modifying slices of them and re-sending to the C drivers.Gowen
You also need to consider your dev platform. Linux is not a problem but windows is restricted to one particular compiler fot CFFI. You will have to check for compiler compatibility with any other packages you are using if you opt for CFFI on WindowsStuartstub

© 2022 - 2024 — McMap. All rights reserved.