Python threading (was: Re: global interpreter lock not working as it should)

Paul Rubin phr-n2002b at NOSPAMnightsong.com
Sun Aug 4 11:58:47 CEST 2002


a-steinhoff at web.de (Armin Steinhoff) writes:
> > The interpreter in Python is effectively a critical region of
> > code. Sections of the interpreter that might block are placed
> > outside of the region so that other threads can enter it while
> > that thread is blocked. Similarly, every thread is forced
> > periodically to leave the region and re-enter it in order to allow
> > the thread scheduler to re-schedule as necessary (the piece you
> > posted).
> 
> IMHO ... there is nothing like a 'thread-scheduler' in the python
> code.  All python threads are scheduled by the OS ... that means
> there is no code in the python program which can _force_
> periodically a thread to leave this critical section ( e.g. in the
> middle of the execution of the 10 byte codes).

I think you're confused about the meaning of "critical section".

A critical section is a region of code where thread switching is
impossible, because the code has a lock set, interrupts are masked,
or something like that.

When the program leaves the critical section by releasing the lock,
thread switches become possible.  Just because they're POSSIBLE
doesn't mean they necessarily HAPPEN.  It just means the OS's
scheduler can decide to switch threads there if it wants to.  It might
not want to.  While in the critical section, the OS doesn't have the
option of switching.  That's the difference.

In Python, the interpreter leaves the critical section (releases the
GIL) every 10 byte codes.  That doesn't mean there has to be a thread
switch then, but just that there MIGHT be one.  Do you understand now?



More information about the Python-list mailing list