Python call c pass a callback function on Linux

Chris Angelico rosuav at gmail.com
Wed Jan 24 17:23:53 EST 2018


On Thu, Jan 25, 2018 at 9:16 AM, Jason Qian via Python-list
<python-list at python.org> wrote:
> Hi,
>
>   I have following code that works fine on windows.
>
> InvocationCB=WINFUNCTYPE(None, c_char_p, c_int)
> submit = lib.submit
> submit.argtypes = [ctypes.c_void_p, c_void_p,InvocationCB]
> submit.restype = ctypes.c_int
>
> def handleResponse(message, code):
>       print('--- handleResponse ---')
>       print(message)
>       print(code)
>
> class GSPythonDriver(object):
>   def submif(self,methodname)
>     invm_fn = InvocationCB(handleResponse)
>     lib.submit(self.obj,methodname,invm_fn)
>
>  How can I do this on the Linux ?

If you're doing a lot of "got this C code, wanna call it from Python",
I recommend looking into Cython and building a wrapper. Should be a
lot less fiddly - and a lot less fragile - than playing with ctypes
for every call you want to make.

ChrisA



More information about the Python-list mailing list