views:

201

answers:

1

Let's say I have a python script which loads a shared library (SL) through ctypes.

  • The SL sets up a pthread T1
  • The python script configures callbacks through the SL i.e. python script calls functions from the SL with references to python callables

alt text

Now, let's say T1 calls a "callback" function, are the following assumptions true:

  • the callback function on the Python side is executed within the context of T1
  • I can use a queue to communicate between T1 and the Python VM
  • I would need to poll the said queue on the Python VM side

I understand all the concepts of threading, shared state etc. but I haven't dug very deep on the multi-threading side with Python. Since there is an adaptation layer which I do not know enough at the moment (ctypes), I am afraid I'll be missing some key aspects of the process.

+1  A: 

Polling the queue isn't normally necessary (you can devote another thread on the Python side to doing blocking .get calls on it), but that's not a big deal. Problem is, with such an arrangement, you might get caught by the GIL -- see the three links from this wikipedia page for ample treatments thereof.

When you interface to/from C with C (or Cython) code using the Python C API, you can release and acquire the GIL pretty simply, at least, hopefully avoiding deadlocks and the like; with ctypes, GIL operations are automated in callback to/from C situations, so if there's any other lock in play a deadlock is a risk (since things are not within your control you can't easily ensure Djikstra's Banker Algorithm is applied).

Alex Martelli
+1: If I understand you correctly, `ctypes` introduces a `thunk` which handles locking/unlocking the GIL to/from the Cython interpreter, right? In other words, the callback python function would effectively executes within the Cython VM, right?
jldupont
@jldupont, Cython doesn't introduce another VM of its own -- rather, it generates C code which (depending on what it's doing) runs on the bare metal or makes calls to the Python VM, just like C code you could write by hand according to Python's C API. `ctypes` is different and does introduce thunks for callbacks to take care of the GIL (convenient, but you get less control). Python functions always execute in the Python VM, and thus need to be holding the GIL while they execute.
Alex Martelli
@Alex: `thunking` there is, I get it now. Thanks!
jldupont