Threading a lengthy C function

Duncan Booth duncan at NOSPAMrcp.co.uk
Thu Nov 20 08:21:13 EST 2003


Leo Breebaart <leo at lspace.org> wrote in news:bpid8s$r95$1 at news.tudelft.nl:

> I'd like to know if this interpretation of what's happening is
> correct (or if it isn't, then what is?), but most importantly I
> was wondering if anybody could point me in the direction of a
> solution or workaround to my actual problem.
> 
Your interpretation sounds spot on.

What you need to do is to release the GIL as the last thing that happens in 
your C code before you call the lengthy function, and reclaim it as the 
first thing you do when the function returns.

It may be easiest simply to wrap the library function in another C function 
that does this, and then use SWIG to wrap the new function instead of the 
original. e.g.

#include <python.h>
int safe_thefunction(char *arg, int arg2)
{
   int res;
   Py_BEGIN_ALLOW_THREADS
   res = thefunction(arg, arg2);
   Py_END_ALLOW_THREADS
   return res;
}

and then wrap safe_thefunction instead of thefunction.

If the function you are wrapping can call back into Python then you must 
also be sure to reclaim the GIL during each callback. This is easy enough 
to do if the callbacks all happen on the same thread, but can be a pain if 
there are asynchronous callbacks on other threads.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list