Thread confusion about Python and Windows

Naerbnic naerbnic at uclink4.berkeley.edu
Tue May 13 15:11:47 EDT 2003


aahz at pythoncraft.com (Aahz) wrote in message news:<b9qvvj$lio$1 at panix1.panix.com>...
> In article <3ec0a481 at usenet.per.paradox.net.au>,
> Cameron Zemek <grom_3 at optusnet.com.au> wrote:
> >
> >Then why on earth is there multi-threaded libraries??
> 
> Because you can call out to non-Python libraries that *do* run
> thread-hot.  It's only pure Python code (or code that uses the Python
> API) that blocks on the GIL.  For example, if you run a multi-threaded
> web spider on an SMP box, socket calls will run simultaneously on
> multiple CPUs.

Furthermore, I/O Blocking operations (such as waiting for data from a
socket, or waiting for data from a file on a disk) will run in
parallel even on a single processor machine. Basically, the system is
just waiting for the hardware to catch up to the software, so there is
no reason for the OS to sit idle. I belive Python is designed around
this philosophy, so such I/O Blocking operations will not be holding
the GIL while they are waiting.

One situation where this can be very useful is having one main thread
handle most of the work, and have a utility thread handle
communications with clients and the like. The latter will be blocking
most of the time, so it won't really slow down your main thread. And
since it's waiting constantly, when a message does finally come down
the pike, it will be fairly fast to respond, and be able to pass the
info to the main thread quickly.

- Brian




More information about the Python-list mailing list