[Python-Dev] working with Python threads from C extension module? (original) (raw)

Bill Janssen janssen at parc.com
Sat Sep 8 17🔞35 CEST 2007


Be convoluted yourself and do this:

#define PySSLBEGINALLOWTHREADS { if (ssllocks) { PyBEGINALLOWTHREADS #define PySSLENDALLOWTHREADS PyENDALLOWTHREADS } } (Untested, but I think it should work.)

Yes, that had occurred to me. We want the code inside the braces still to run if the locks aren't held, so something more like

#define PySSL_BEGIN_ALLOW_THREADS {
PyThreadState *_save;
if (_ssl_locks_count>0) {_save = PyEval_SaveThread();} #define PySSL_BLOCK_THREADS if (_ssl_locks_count>0){PyEval_RestoreThread(_save)}; #define PySSL_UNBLOCK_THREADS if (_ssl_locks_count>0){_save = PyEval_SaveThread()}; #define PySSL_END_ALLOW_THREADS if (_ssl_locks_count>0){PyEval_RestoreThread(_save);}
}

would do the trick. Unfortunately, this doesn't deal with the macro behaviour. The user has "turned on" threading; they expect reads and writes to yield the GIL so that other threads can make progress. But the fact that threading has been "turned on" after the SSL module has been initialized, means that threads don't work inside the SSL code. So the user's understanding of the system will be broken.

No, I don't see any good way to fix this except to add a callback chain inside PyThread_init_thread, which is run down when threads are initialized. Any module which needs to set up threads registers itself on that chain, and gets called as part of PyThread_init_thread. But I'm far from the smartest person on this list :-), so perhaps someone else will see a good solution.

This has got to be a problem with other extension modules linked to libraries which have their own threading abstractions.

Bill



More information about the Python-Dev mailing list