[Pyrex] Py_BEGIN_ALLOW_THREADS

Simon Burton simon at arrowtheory.com
Tue Jun 29 10:26:00 CEST 2004


On Mon, 28 Jun 2004 13:13:53 -0400
Itamar Shtull-Trauring <itamar at itamarst.org> wrote:

> How would one use that in pyrex code?
> 

You have to expand the macro.

Check out sec 8.1 in the Python/C API Reference Manual:
....
This is so common that a pair of macros exists to simplify it:

Py_BEGIN_ALLOW_THREADS
...Do some blocking I/O operation...
Py_END_ALLOW_THREADS

The Py_BEGIN_ALLOW_THREADS macro opens a new block and declares a hidden local variable; the Py_END_ALLOW_THREADS macro closes the block. Another advantage of using these two macros is that when Python is compiled without thread support, they are defined empty, thus saving the thread state and lock manipulations.

When thread support is enabled, the block above expands to the following code:

    PyThreadState *_save;

    _save = PyEval_SaveThread();
    ...Do some blocking I/O operation...
    PyEval_RestoreThread(_save);

Simon.

-- 
Simon Burton, B.Sc.
Licensed PO Box 8066
ANU Canberra 2601
Australia
Ph. 61 02 6249 6940
http://arrowtheory.com 




More information about the Pyrex mailing list