[LWN Logo]

From: wware-nospam@world.std.com (Will Ware)
Subject: Python microthreads (experimental)
Date: Mon, 02 Aug 99 02:39:11 GMT

I have been interested in using Python to program simulated
swarms of interacting agents. For this purpose, I have implemented
what I call "microthreads" in Python, modifying Python's virtual
machine, and maintaining a frame stack for each thread. I have
successfully run 1500 threads simultaneously. Source code and some
example usages are available at

  ftp://ftp.std.com/pub/wware/uthread.tgz

for anybody interested in this. This is a pre-pre-thinking-about-
alpha-some-day release, and probably buggy, but the examples run
as expected.

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

[Moderator's note: the following is taken from the package's README file.]

**************************************************************************

Microthreads for Python
Will Ware <wware@world.std.com>

It would be nice to use Python for programs involving huge numbers of
very light-weight threads. This would make it easy to simulate large
swarms of interacting agents, which sounds like fun.

Python functions are compiled to a bytecode, which runs on a virtual
machine, defined in Python/ceval.c in the eval_code2 function. Python
uses frame objects to administer information involved in nested
function calls. In ceval.c, the frame stack is implicitly embedded in
the C stack, because eval_code2 calls itself recursively when one
Python function calls another.

Microthreads are implemented with a modified version of the eval_code2
function. This version allows different threads to maintain different
frame stacks explicitly. This version also executes a predetermined
number of opcodes, whereas eval_code2 normally runs an infinite loop,
terminating only when a function reaches either an error or a return
statement.

Two data structures have been defined. One is a thread object, which
includes a frame stack, and also stacks for program counters and stack
pointers. The other is a semaphore object, which provides an ability
for threads to coordinate with one another. Using a semaphore it is
possible to define a mailbox in Python, whereby one thread can send
messages to another.

The included example files demonstrate how threads and semaphores
work, and how to implement a mailbox.

**************************************************************************

<P><A HREF="ftp://ftp.std.com/pub/wware/">Python microthreads</A> - 
experimental implementation of interpreter-level threads using a
modified Python byte code interpreter.  (01-Aug-99)

-- 
----------- comp.lang.python.announce (moderated) ----------
Article Submission Address:  python-announce@python.org
Python Language Home Page:   http://www.python.org/
Python Quick Help Index:     http://www.python.org/Help.html
------------------------------------------------------------