Calling into Python from a C thread

Philip Semanchuk philip at semanchuk.com
Mon Feb 2 13:03:20 EST 2009


Hi all,
I'm trying call Python from inside of a C thread that's running in a  
Python extension I've written and I am not having much luck. My C  
thread function consists of simply this, and I get a segmentation  
fault from Python:

void start_routine(union sigval foo) {
     PyGILState_STATE gstate;

     gstate = PyGILState_Ensure();

     // Calls to Python code will go here...

     PyGILState_Release(gstate);
};

I added a printf() and can see that the fault happens *after* the call  
to PyGILState_Release(). Apparently I'm monkeying up something  
fundamental in Python, and this is really simple so I must be missing  
something big.

The context is that I'm adding support for mq_notify() to my posix_ipc  
extension. A process can register via mq_notify() to have a new thread  
started when a message appears in a message queue. I've got this  
working when my thread code is pure C, now I'm trying to add a call to  
a user-specified Python function.

Thanks
Philip



More information about the Python-list mailing list