Summary of threading for experienced non-Python programmers?

Hrvoje Niksic hniksic at xemacs.org
Fri Mar 28 12:39:01 EDT 2008


skip at pobox.com writes:

> I'm having trouble explaining the benefits and tradeoffs of threads
> to my coworkers and countering their misconceptions about Python's
> threading model and facilities.  They all come from C++ and are used
> to thinking of multithreading as a way to harness multiple CPU cores
> for compute-bound processing.  I also encountered, "Python doesn't
> really do threads" today.  *sigh*

Compute-bound processing pretty much excludes a Python-only solution,
so any performance considerations should take into account C
extensions.  Extensions are free to allow other threads to run during
CPU-extensive portions of their work, and many of them in fact do so.
As long as the extensions are correctly written, you can write your
"glue code" in Python and harness the multiple cores using threads,
exactly as expected by a C++ programmer.

The other use for threads is the case when dealing with blocking APIs
that don't support polling.  These typically include database APIs and
some network APIs (such as the portable host name lookup), but also
basic file input/output, if you take into account network file
systems.  (In theory, file input/output should also be available as
asynchronous code, but async IO is low-level and not available in
Python.)  While threads shouldn't be considered a replacement for
event-driven programming, they are certainly useful in such
situations.



More information about the Python-list mailing list