2000Q1/
<!-- MHonArc v2.4.4 -->
<!--X-Subject: [MUD&#45;Dev] Microthreads for Python -->
<!--X-From-R13: X Q Znjerapr <pynjNxnatn.ah> -->
<!--X-Date: Mon, 03 Jan 2000 13:42:17 &#45;0800 -->
<!--X-Message-Id: E125FEv&#45;0000ih&#45;00#dingo,kanga.nu -->
<!--X-Content-Type: text/plain -->
<!--X-Head-End-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>MUD-Dev message, [MUD-Dev] Microthreads for Python</title>
<!-- meta name="robots" content="noindex,nofollow" -->
<link rev="made" href="mailto:claw#kanga,nu">
</head>
<body background="/backgrounds/paperback.gif" bgcolor="#ffffff"
      text="#000000" link="#0000FF" alink="#FF0000" vlink="#006000">

  <font size="+4" color="#804040">
    <strong><em>MUD-Dev<br>mailing list archive</em></strong>
  </font>
      
<br>
[&nbsp;<a href="../">Other Periods</a>
&nbsp;|&nbsp;<a href="../../">Other mailing lists</a>
&nbsp;|&nbsp;<a href="/search.php3">Search</a>
&nbsp;]
<br clear=all><hr>
<!--X-Body-Begin-->
<!--X-User-Header-->
<!--X-User-Header-End-->
<!--X-TopPNI-->

Date:&nbsp;
[&nbsp;<a href="msg00043.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00045.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Thread:&nbsp;
[&nbsp;<a href="msg00047.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00042.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Index:&nbsp;
[&nbsp;<A HREF="author.html#00044">Author</A>
&nbsp;|&nbsp;<A HREF="#00044">Date</A>
&nbsp;|&nbsp;<A HREF="thread.html#00044">Thread</A>
&nbsp;]

<!--X-TopPNI-End-->
<!--X-MsgBody-->
<!--X-Subject-Header-Begin-->
<H1>[MUD-Dev] Microthreads for Python</H1>
<HR>
<!--X-Subject-Header-End-->
<!--X-Head-of-Message-->
<UL>
<LI><em>To</em>: <A HREF="mailto:mud-dev#kanga,nu">mud-dev#kanga,nu</A></LI>
<LI><em>Subject</em>: [MUD-Dev] Microthreads for Python</LI>
<LI><em>From</em>: J C Lawrence &lt;<A HREF="mailto:claw#kanga,nu">claw#kanga,nu</A>&gt;</LI>
<LI><em>Date</em>: Mon, 03 Jan 2000 13:42:09 -0800</LI>
<LI><em>Reply-To</em>: <A HREF="mailto:mud-dev#kanga,nu">mud-dev#kanga,nu</A></LI>
<LI><em>Sender</em>: <A HREF="mailto:mud-dev-admin#kanga,nu">mud-dev-admin#kanga,nu</A></LI>
</UL>
<!--X-Head-of-Message-End-->
<!--X-Head-Body-Sep-Begin-->
<HR>
<!--X-Head-Body-Sep-End-->
<!--X-Body-of-Message-->
<PRE>

While Python already supports threads, its not really a fully
scalable model.  The following addresses some of that:

  <A  HREF="http://world.std.com/%7ewware/uthread.html">http://world.std.com/%7ewware/uthread.html</A>

--&lt;cut&gt;--
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 code in
Python/ceval.c, which is Python's bytecode interpreter. Each thread
maintains an explicit frame stack, whereas normally frames are kept
on the C stack via recursive calls to eval_code2. My version of
eval_code2 can be stepped for a number of steps, unlike the normal
version which runs in an infinite loop until the outermost function
returns.

I've defined several objects in C: a "working" object (which is a
placeholder like the normal "None" object), a "uthread" object, a
"semaphore" object (also known as a mutex), and a "queue"
object. The semaphore and queue are microthread-safe. There is also
a microthread-safe version of the whrandom random number generator.

uthread.thread() creates a microthread. As arguments, it takes a
function, and any arguments that the function expects. For instance
to create a thread that computes f(x,y,z), you would write something
like 'mythread = uthread.thread(f,x,y,z)'.

Every thread has a step method (mythread.step()) which will step the
microthread for a finite number of opcodes. It returns the thread's
return value, if the thread has finished, or if the thread has not
yet finished, it returns uthread.working (the "working" placeholder
object). If you attempt to step a thread which has already finished,
an error will result.

uthread.semaphore() creates a semaphore object. It takes an optional
integer argument (default value is 1) telling how many instances of
the semaphore may be outstanding at any moment. A semaphore has the
following methods: claim() will claim one instance of a semaphore,
or block until an instance becomes available. qclaim() is a
non-blocking version of claim, which returns a boolean telling
whether the claim was successful. release() will release an instance
of the semaphore.

uthread.queue() creates a queue object, and takes an integer
argument, specifying a number of stages.  Each stage in the queue is
a normal list. A queue has the following methods: put(x) will append
x to the list at the rear of the queue, get() will remove and return
the first object from the list at the front of the queue (None if
the front queue is empty), and step() will advance the lists one
step toward the front of the queue, concatenating the front two
lists to become the new front list.

There is much similarity between this project and Stackless Python
by Christian Tismer, as several people on the Python newsgroup have
pointed out. Eventually it will probably make sense to merge
microthreads into Stackless Python, but it's not my most immediate
priority.

Some of the applications that interest me for microthreads are
amorphous computing and computational economics. Microthreads could
also be used to write agents in a game, such as robot tanks or
PacMan monsters
--&lt;cut&gt;--

The Stackless Python referenced above can be found at:

  <A  HREF="http://www.pns.cc/stackless/stackless.htm">http://www.pns.cc/stackless/stackless.htm</A>

but currently that site is note accepting connections.

-- 
J C Lawrence                                 Home: claw#kanga,nu
----------(*)                              Other: coder#kanga,nu
--=| A man is as sane as he is dangerous to his environment |=--


_______________________________________________
MUD-Dev maillist  -  MUD-Dev#kanga,nu
<A  HREF="http://www.kanga.nu/lists/listinfo/mud-dev">http://www.kanga.nu/lists/listinfo/mud-dev</A>

</PRE>

<!--X-Body-of-Message-End-->
<!--X-MsgBody-End-->
<!--X-Follow-Ups-->
<HR>
<!--X-Follow-Ups-End-->
<!--X-References-->
<!--X-References-End-->
<!--X-BotPNI-->
<UL>
<LI>Prev by Date:
<STRONG><A HREF="msg00043.html">Re: [MUD-Dev] Embedded languages, object persistance... ack.</A></STRONG>
</LI>
<LI>Next by Date:
<STRONG><A HREF="msg00045.html">Re: [MUD-Dev] Storing tokens with flex &amp; bison</A></STRONG>
</LI>
<LI>Prev by thread:
<STRONG><A HREF="msg00047.html">[MUD-Dev] For those interested in parsers and compilers</A></STRONG>
</LI>
<LI>Next by thread:
<STRONG><A HREF="msg00042.html">[MUD-Dev] Catalog of Compiler Construction Tools</A></STRONG>
</LI>
<LI>Index(es):
<UL>
<LI><A HREF="index.html#00044"><STRONG>Date</STRONG></A></LI>
<LI><A HREF="thread.html#00044"><STRONG>Thread</STRONG></A></LI>
</UL>
</LI>
</UL>

<!--X-BotPNI-End-->
<!--X-User-Footer-->
<!--X-User-Footer-End-->
<ul><li>Thread context:
<BLOCKQUOTE><UL>
<LI><strong><A NAME="00049" HREF="msg00049.html">[MUD-Dev] EQ packet analyzer is gone?</A></strong>, 
Sellers, Michael <a href="mailto:MSellers#maxis,com">MSellers#maxis,com</a>, Tue 04 Jan 2000, 17:51 GMT
<UL>
<LI><strong><A NAME="00051" HREF="msg00051.html">Re: [MUD-Dev] EQ packet analyzer is gone?</A></strong>, 
J C Lawrence <a href="mailto:claw#cp,net">claw#cp,net</a>, Tue 04 Jan 2000, 23:56 GMT
</LI>
<LI><strong><A NAME="00125" HREF="msg00125.html">Re: [MUD-Dev] EQ packet analyzer is gone?</A></strong>, 
J C Lawrence <a href="mailto:claw#kanga,nu">claw#kanga,nu</a>, Tue 18 Jan 2000, 09:26 GMT
</LI>
</UL>
</LI>
<LI><strong><A NAME="00047" HREF="msg00047.html">[MUD-Dev] For those interested in parsers and compilers</A></strong>, 
J C Lawrence <a href="mailto:claw#kanga,nu">claw#kanga,nu</a>, Tue 04 Jan 2000, 07:03 GMT
<LI><strong><A NAME="00044" HREF="msg00044.html">[MUD-Dev] Microthreads for Python</A></strong>, 
J C Lawrence <a href="mailto:claw#kanga,nu">claw#kanga,nu</a>, Mon 03 Jan 2000, 21:42 GMT
<LI><strong><A NAME="00042" HREF="msg00042.html">[MUD-Dev] Catalog of Compiler Construction Tools</A></strong>, 
J C Lawrence <a href="mailto:claw#kanga,nu">claw#kanga,nu</a>, Mon 03 Jan 2000, 19:35 GMT
<LI><strong><A NAME="00040" HREF="msg00040.html">Re: Re[6]: [MUD-Dev] The grass is always greener in the other field</A></strong>, 
Adam Wiggins <a href="mailto:adam#angel,com">adam#angel,com</a>, Mon 03 Jan 2000, 19:02 GMT
<LI><strong><A NAME="00039" HREF="msg00039.html">[MUD-Dev] EQ packet sniffer</A></strong>, 
J C Lawrence <a href="mailto:claw#kanga,nu">claw#kanga,nu</a>, Mon 03 Jan 2000, 18:12 GMT
<LI><strong><A NAME="00033" HREF="msg00033.html">[MUD-Dev] Library submission notification and updates</A></strong>, 
J C Lawrence <a href="mailto:claw#kanga,nu">claw#kanga,nu</a>, Mon 03 Jan 2000, 16:40 GMT
</UL></BLOCKQUOTE>

</ul>
<hr>
<center>
[&nbsp;<a href="../">Other Periods</a>
&nbsp;|&nbsp;<a href="../../">Other mailing lists</a>
&nbsp;|&nbsp;<a href="/search.php3">Search</a>
&nbsp;]
</center>
<hr>
</body>
</html>