global interpreter lock not working as it should

anton wilson anton.wilson at camotion.com
Mon Jul 29 19:49:00 EDT 2002


On Monday 29 July 2002 07:35 pm, Martin v. Loewis wrote:
> anton wilson <anton.wilson at camotion.com> writes:
> > I'm having a problem where the interpreter in ceval.c does not let
> > threads run concurently. Any thread that holds the lock never gives
> > up the lock until it has run to completion.
>
> I don't believe that statement. Do you have an example program that
> demonstrates you claims?
>
> To demonstrate my point, consider
>
> import threading, time
>
> def func1():
>     for i in range(3):
>         print "FUNC1"
>         time.sleep(1)
>
> def func2():
>     for i in range(3):
>         print "FUNC2"
>         time.sleep(1)
>

The reason your code works is because you explicitly give up the CPU
with the sleep calls. If you remove both time.sleep calls the results will 
look like:

FUNC2
FUNC2
FUNC2
FUNC1
FUNC1
FUNC1


> t = threading.Thread(target=func1)
> t.start()
> func2()
>
> On my Linux system, it prints
>
> FUNC2
> FUNC1
> FUNC1
> FUNC2
> FUNC2
> FUNC1
>
> Regards,
> Martin




More information about the Python-list mailing list