Use of Py_BEGIN_ALLOW_THREADS?

Bill Eldridge bill at rfa.org
Mon May 7 12:33:59 EDT 2001


>In article <3AD42A5F.73AE10EC at rfa.org>, Bill Eldridge  <bill at rfa.org> >wrote:
>>
>>I'm calling a C function from Python, and that C function in turn calls
>>another C function #2 with a C callback function.
>>
>>Somewhere it seems like with threads it disappears - all of the debug
>>messages in function #2 come out, but the C callback never gets called.
>
>So you're saying that this works correctly if you don't use threads?

I'm saying it works correctly if I have a C main program
instead of a Python main, with the C being as simple as:

void main() {
   call_audio_stuff();
}

I didn't try using the Py_BEGIN_ALLOW_THREADS until
my initial try with a Python main program didn't work.

>What happens if you have multiple threads, but only one thread calls the
>original C function?  My suspicion is that you're getting cross-thread
>contamination in your C code because it isn't thread-safe.  

I can try doing the call_audio_stuff as an individual thread
from Python...

>Note that you should *ONLY* use Py_BEGIN_ALLOW_THREADS if your code is
>"thread hot"; that is, not just thread-safe, but designed to allow
>multiple threads into the same code at the same time.  In the absence of
>Py_BEGIN_ALLOW_THREADS, your C code is considered to be one Python
>bytecode and executes as a single unit.
> 
>                      --- Aahz  <*>  (Copyright 2001 by aahz at pobox.com)

How exactly do I make it "thread hot"?
I'm not specifically sure why I need the threads at all,
as some have indicated to me.  Again, the thing that's
failing follows:

--------------------------------------------
#!/usr/bin/python

import my_audio

my_audio.do_something()
---------------------------------------------

void main() {
   do_audio_stuff();
}

void do_audio_stuff() {
   call_callback(item1,item2,&c_callback);
}

void c_callback(...) {
   printf("Got called\n");
}




--
Bill Eldridge
Radio Free Asia
bill at rfa.org



More information about the Python-list mailing list