I want to release the GIL

Piotr Sobolewski piotr_sobolewski at o2.pl
Tue Oct 21 07:12:15 CEST 2008


Hello,
I have such program:

import time
import thread
def f():
    global lock
    while True:
        lock.acquire()
        print thread.get_ident()
        time.sleep(1)
        lock.release()
lock=thread.allocate_lock()
thread.start_new_thread(f,())
thread.start_new_thread(f,())
time.sleep(60)

As you can see, I start two threads. Each one works in an infinite
loop.
Inside that loop it acquires lock, prints its own id, sleeps a bit and
then
releases lock.

When I run it, I notice that only one thread works and the other one
never
has a chance to run. I guess it is because the thread don't have a
chance
to release the GIL - after it releases the lock, it almost immediately
(in
the very next bytecode command) reacquires it. I know I can
put "time.sleep(0.01)" command after between release and reacquire,
but it
doesn't seem elegant - why should I make my program sleep instead of
work?

Is there any simple way to release the GIL? Like:
lock.release()
thread.release_gil()
?

Thanks in advance!



More information about the Python-list mailing list