Martin Gysel wrote:
> Robert Bradshaw schrieb:
>> On Mar 18, 2009, at 2:17 PM, Martin Gysel wrote:
>>
>>
>>> Hi
>>>
>>> I have a 'cython' class which interfaces c code. therefor I need some
>>> callback functions which I declared outside of the class. Over a
>>> userdata argument pointer this function gets the reference of my
>>> class.
>>> Callback registration and also the callback itself work. The
>>> problem is,
>>> as soon as the callback starts to execute, I'll get a segfault (from
>>> time to time it also works...). So I debugged the c code, the segfault
>>> occurs while getting the sdtout for the print statement. So I removed
>>> all print statements but from my callback I want to call either a
>>> function of my class or some python callback (which needs to be
>>> registered first). For me it seems the segfault occurs when accessing
>>> some python objects. I also tried to play around with 'which gil' and
>>> 'nogil' but no luck. When using 'which gil' it segfaults
>>> immediately the
>>> callback gets called.
>>> What do I wrong? I assume it should be possible to have print in
>>> callback functions and also to have access to python objects.
>>>
>>
>> Yes, that should be possible. It does sound like you're having
>> threading/gil issues. What is your callback function signature? When
>> you say you've tried "with gil" are you doing
>>
>> cdef void my_callback(void *data) with gil:
>> ...
>>
> yes exactly (well some more arguments...). I forgot to mention that the
> callback gets called from another thread as the library which I want to
> interface starts such a thread. Probably thats the point. But how can I
> assure that another thread can safely call my code?
The above should work. Can you tell us a bit more about the C code that
calls your callback? Is there a direct call chain like
your code
-> C function with callback pointer
-> ...
-> callback
-> your code
...
<-
your code
or do you just register your callback and it gets called at some later point?
In the first case, you need to release the GIL by wrapping the C function
call in a "with nogil:" block, so that the callback can acquire it. The
second case should just work, but you have to make sure that you release
the GIL from time to time to allow the callback to run (the Python
interpreter does that, but only when it's running Python code).
Stefan
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev