python's threading has no "interrupt"?

Aahz aahz at pythoncraft.com
Tue Dec 2 20:26:02 EST 2003


In article <9btu91-s04.ln1 at beastie.ix.netcom.com>,
Dennis Lee Bieber  <wlfraed at ix.netcom.com> wrote:
>
>There's the first point of departure: Python threads (as long as you
>aren't in a C language number crunching extension) are preemptive
>scheduled, on something like a 10-20 byte-code interval. Cooperative
>basically means /you/ had to handle the scheduling of threads; in
>Python you don't, it happens automatically.

Actually, that's not really correct.  Cooperative threading means that
there's no way to force a thread to yield up a timeslice, which is
precisely the way Python works, because any bytecode can last an
arbitrarily long time (consider 100**100**100).  It is true that the
Python core does switch between bytecodes, but that should not be
considered preemptive in the traditional sense.  OTOH, Python threads
are built on top of OS-level threads, so extensions that release the GIL
do run under the preemptive OS scheduler.  To put it another way, the
reason why Python threads aren't preemptive is because each Python
thread acquires an OS-level lock that only the Python core will release,
one thread at a time.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote 
programs, then the first woodpecker that came along would destroy civilization.




More information about the Python-list mailing list