releasing interpreter lock in custom code?

B

Bram Stolk

Hello,


I've implemented, in C, a function that does a lot of I/O, and thus
can block for a long time.

If I execute this function in my Python script, it does not
relinquish the global interpreter lock, like Python's native
blocking functions do, like I/O funcs, and time.sleep() func.

How can I have my func release the lock?

thanks!

Bram Stolk
 
A

Andrew MacIntyre

Bram said:
I've implemented, in C, a function that does a lot of I/O, and thus
can block for a long time.

If I execute this function in my Python script, it does not
relinquish the global interpreter lock, like Python's native
blocking functions do, like I/O funcs, and time.sleep() func.

How can I have my func release the lock?

Search the Python documentation (Python/C API section) for information
about the Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS macros.

Perusing Python's source would also be educational (eg
Modules/posixmodule.c).

--
 
S

Sion Arrowsmith

Bram Stolk said:
I've implemented, in C, a function that does a lot of I/O, and thus
can block for a long time.

If I execute this function in my Python script, it does not
relinquish the global interpreter lock, like Python's native
blocking functions do, like I/O funcs, and time.sleep() func.

How can I have my func release the lock?

http://docs.python.org/api/threads.html is the first hit on
searching docs.python.org for "global interpreter lock". It
says:

"""
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
"""
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top