I want to release the GIL
Delaney, Timothy (Tim)
tdelaney at avaya.com
Tue Oct 21 08:42:12 CEST 2008
Piotr Sobolewski wrote:
> I have such program:
> import time
> import thread
> def f():
> global lock
> while True:
> print thread.get_ident()
1. You should use the threading module.
2. No need for the "global lock" statement here - you're not rebinding the name "lock".
3. These aren't daemon threads, so your program will never exit. You will need to set a flag or something after the time.sleep(60).
> As you can see, I start two threads. Each one works in an infinite
> Inside that loop it acquires lock, prints its own id, sleeps a bit and
> releases lock.
4. Because you are holding the lock while sleeping, the other thread does not get the chance to run. Sleeping does not release any locks held.
Instead try something like:
Note that to use the "with lock:" idiom, you need to be using Python 2.6, or Python 2.5 with a "from __future__ import with_statement".
More information about the Python-list