Stefan Behnel schrieb: > 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). > It's the second case, I register it and it gets called at some time. As already said, the library starts a new thread... at the time the callback gets called no cython code is supposed to run except the callback itself of course.
1. register callback 2. start library thread 3. ... (at the moment I just do nothing apart of waiting) 4. callback gets called (here I want to deal with either the library or my cython object) 5. segfault :-( really strange for me is it segfaults immediately if I use with gil. is it possible the gil is held by another function? /martin _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
