time and thread modules.

hg hansgeunsmeyer at earthlink.net
Fri Oct 11 02:51:12 CEST 2002


sismex01 at hebmex.com wrote in message news:<mailman.1034265014.21912.python-list at python.org>...

> My question comes from the fact that there's Python's GIL; so,
> if I sleep() a task, which internally calls the OS's sleep()
> syscall, which then yields control to another thread... What happens
> in the Python side of the equation?
> 
> Is another of Python's thread's scheduled?
> 
> Does the same thread keep the GIL, which in fact precludes
> any other of python's threads from running until sleep()
> returns?

If you look at the implementation of time.sleep in timemodule.c,
you'll see that before actually calling Sleep (or select, or whatever
the function is, depending on your platform), Python uses the
Py_BEGIN_ALLOW_THREADS macro which
saves the current thread state, and releases the GIL (MacIntosh code
is a bit different here, but I suppose the effect is the same).  Once
the GIL is released some other thread may grap it.

Hans



More information about the Python-list mailing list