<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2900.3020" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>Greetings!</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>I'm quite new to Stackless and this is my first
post here. ::waves:: I'm also fairly new to Twisted and am returning to
Python after a long absence. My goal is to is to use these three
technologies along with the Torque Game Engine to build a multiplayer
game. I'm currently in the technology testing stage.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>I've recently gone through the exercise myself of
writing a custom Twisted reactor for use with Stackless (under Python
2.4). After looking at what has been proposed on the Twisted mailing list,
I took the direction of Stackless driving Twisted rather than the other way
around. After combining some techniques from Josh Ritter (over at
GarageGames), Richard Tew (uthread is cool) and other sources, here's what
I've come up with.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>So far my own testing indicates everything is
working as expected. But it would be great to know if anyone finds any
issues with my implementation. And hopefully this'll help Andrew
out. I've included my stackless reactor as well as a snippet on how to use
it. You'll have to provide your own Twisted-based HTTP server if you want
to run the example as is.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>--- stacklessreactor.py ---</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>__all__ = ['install']<BR>import sys<BR>import
stackless<BR>import uthread<BR>from twisted.internet import
selectreactor</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>gReactor = None</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>def ReactorTick():<BR> global
gReactor<BR> while(gReactor.running !=
0):<BR>
gReactor.simulate()<BR>
uthread.BeNice()</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>class
StacklessReactor(selectreactor.SelectReactor):<BR> def
__init__(self):<BR>
super(StacklessReactor,
self).__init__()<BR> self.stopped =
False<BR> <BR> def
crash(self):<BR> print
"StacklessReactor crash!"<BR>
self.running = 0<BR> self.stopped =
True<BR> <BR> def
run(self):<BR> print "StacklessReactor
starting to run"<BR>
self.startRunning()<BR>
stackless.tasklet(ReactorTick)()<BR>
<BR> def
simulate(self):<BR>
self.iterate()<BR> <BR>def
StacklessReactorInstall():<BR> global gReactor</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> print "StacklessReactorInstall()
executed"<BR> <BR> reactor =
StacklessReactor()<BR> gReactor =
reactor<BR> <BR> from twisted.internet.main
import installReactor<BR>
installReactor(reactor)<BR> return reactor</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>install = StacklessReactorInstall<BR></FONT></DIV>
<DIV><FONT face=Arial size=2>--- maintest.py ---</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>import stackless<BR>import uthread</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>def ServerStartup():<BR> from
twisted.internet import reactor<BR> from httpserver import
MyHttpFactory<BR> <BR> print "Starting up
the HTTP server"<BR> reactor.listenTCP(8001,
MyHttpFactory())<BR> reactor.run()<BR>
<BR>def run():<BR> """Replacement
for uthread.run()"""<BR> while(reactor.stopped ==
False):<BR>
uthread.RunNiceTasklets()<BR> t =
stackless.run(10000000)<BR> if t is
not None:<BR>
# Need better standard handling of this
case.<BR> #
Could StackTrace I
guess?<BR>
raise RuntimeError("Runaway tasklet",
t)<BR>
uthread.CheckSleepingTasklets()</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2><BR>if __name__ ==
"__main__":<BR> import stacklessreactor<BR>
stacklessreactor.install()</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> from twisted.internet import
reactor<BR> <BR> print "Running
reactor"<BR>
stackless.tasklet(ServerStartup)()<BR>
run()<BR> print "Finished running reactor"<BR></FONT></DIV>
<DIV><FONT face=Arial size=2>------</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>The only other thing I was going to add is the
handling of Twisted deferreds using the same method as suggested in <A
href="http://twistedmatrix.com/trac/browser/sandbox/radix/threadless.py">http://twistedmatrix.com/trac/browser/sandbox/radix/threadless.py</A> blockOn()
function.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Hope that helps!</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>- Dave</FONT></DIV>
<DIV><FONT face=Arial size=2><A
href="http://www.gnometech.com">http://www.gnometech.com</A></FONT></DIV>
<DIV><FONT face=Arial size=2> </DIV></FONT></BODY></HTML>