multithread programming in python?

Will Ware wware at world.std.com
Wed Mar 29 20:19:18 CEST 2000


oliver (fwang2 at yahoo.com) wrote:
> I just begin to look at python, like its clean syntax and high level data
> type, I need to prototype a simulation which involve multiple threads and
> timer, I wonder if I can achieve that in Python?

You have two options (that I know about). One is to use thread support
provided by the operating system. This might require rebuilding Python
from source; I don't know if thread-enabled binaries are available on
the ftp site (ftp.python.org, if memory serves). You'll want to look
at the "thread" and "threading" modules in the library reference, which
are documented at
http://www.python.org/doc/current/lib/module-thread.html
and
http://www.python.org/doc/current/lib/module-threading.html

The other option is microthreads, wherein threading is implemented by
tweaking the execution order of Python's virtual machine, rather than
by interrupting the processor. Documentation for this is at
http://world.std.com/~wware/uthread.html

The microthread approach is much newer and much less thoroughly tested
but it might be more straightforward for your application. You'd need
to pick up a recent version of Stackless Python (http://www.stackless.com)
to use it. With microthreads, all your simulation threads run within a
single operating system thread.

The restrictions on microthreads (not shared by OS threads) are that
they will only provide context-switching within Python code, not within
C or Fortran extensions, and they won't help you to take advantage of
multiple processors. Also microthreads can hang on some blocking I/O
operations; they are new enough that there isn't yet a lot of practical
experience with which I/O (or other) operations are troublesome.

-- 
 - - - - - - - - - - - - - - - - - - - - - - - -
Resistance is futile. Capacitance is efficacious.
Will Ware	email:    wware @ world.std.com



More information about the Python-list mailing list