I want to release the GIL

Chris Rebert clp at rebertia.com
Tue Oct 21 02:50:41 EDT 2008


On Mon, Oct 20, 2008 at 10:12 PM, Piotr Sobolewski
<piotr_sobolewski at o2.pl> wrote:
> 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?

You let *one thread* sleep so that the *other thread* can wake, grab
the lock, and *work*.

Cheers,
Chris
-- 
Follow the path of the Iguana...
http://rebertia.com

>
> Is there any simple way to release the GIL? Like:
> lock.release()
> thread.release_gil()
> ?
>
> Thanks in advance!
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list