Callback from c thread with ctypes

Diez B. Roggisch deets at nospam.web.de
Sun Mar 8 09:56:31 EDT 2009


Victor Lin schrieb:
> Hi,
> 
> I am going to develop a c library binding with ctypes. That c library
> will call callback from worker threads it created. Here comes the
> problem : Will the GIL be acquired before it goes into Python
> function?
> 
> I got a little try..
> 
> DSPPROC = WINFUNCTYPE(None, DWORD, DWORD, c_void_p, DWORD, c_void_p)
> 
> def test(handle, channel, buffer, length, user):
>     print handle, channel, buffer, length, user
> 
> dsp = BASS_ChannelSetDSP(stream, DSPPROC(test), None, 123)
> 
> I got "access violation" when I run it... It seems that the ctypes
> did't acquire GIL before it call the python callback. As the document
> says.
> 
> WINFUNCTYPE will release GIL during the call
> 
> But it does not mention callback about Python function? How about a
> call from another thread? Could somebody help me?

The releasing takes only place when entering a c-function. A 
python-callback acquires the GIL, see callbacks.c in the python source.

The access violation has nothing to do with that I presume, that's just 
a general programming error as it happens with ctypes during development.

Diez



More information about the Python-list mailing list