[Stackless] [Twisted-Python] Stackless/Twisted integration again

Andrew Francis andrewfr_ice at yahoo.com
Sun Apr 6 20:23:48 CEST 2008

Hi Simon and Colleages: 

>I note he recommends running a twisted server as a

I do this so both the Twisted reactor and the
Stackless scheduler can run without blocking each

The Twisted Reactor and the Stackless Schedule start
their respective systems. Both the Stackless and
Twisted run() method assume that the application has
finished once the run() method returns.

Here is simple example

import stackless
from   twisted.internet    import reactor

def hello():
    print "hello mom"


This programme will hang. Why? The Twisted reactor is
running in the main tasklet and returns only after it
has been stopped (which is never in this example).
Only after the reactor has finished, will the
stackless scheduler start. Of course, this is not the
behaviour we want.

>factory = pb.PBClientFactory
>reactor.connectTCP('', 1234, Factory)
># Load other tasklets here

I have never used iterate(). I am having a hard time
finding an example of iterate() in action. Perhaps I
am wrong but I would assume iterate() is used in
conjunction with something like reactor.interleave()
so that something like a GUI can function properly.

>From my understanding, your example would produce
something because in the iteration followed by a one
second delay, the client can actually complete a call.
However this seems accidental.

>factory = pb.PBClientFactory
>reactor.connectTCP('', 1234, Factory)
># Load other tasklets here
>while 1:
>    stackless.tasklet(reactor.iterate)(1)
>    stackless.schedule()

I don't understand iterate() well enough but I think
this will accidentally work sometimes. I don't have
time to run your programme but off hand, this is what
I think is happening:

1) Reactor tasklet is created. Since other tasklets
are ahead of it, it is not running.

2) stackless.schedule() kicks off the scheduling,
yielding the main tasklet and allowing the next
schedulable tasklet to run

3) Eventually the Twisted reactor runs. It does stuff.
You get results. However I don't believe it will yield
after that.

>I want to make the reactor a tasklet so it is
included >in the scheduler. 

Simon, stackless.tasklet(reactor.run)() will make the
Twisted reactor into a tasklet. However you need a way
to make it yield occasionally. That is why I use
task.LoopingCall(stackless.schedule). One nice feature
of this approach is that I do not have to alter the

I remember playing with PB a while ago. Here is the


I am enclosing some sample code so you can play with
it. The code may be a bit clunky because I was still
learning. Heck I am still learning :-)


P.S - You may want to take out the 61 second sleep in
the PBServer. And it was neat to actually meet Bruce
Eckel at PyCon.

You rock. That's why Blockbuster's offering you one month of Blockbuster Total Access, No Cost.  
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PBExample.py
Type: text/x-python
Size: 2814 bytes
Desc: 3974082329-PBExample.py
URL: <http://www.stackless.com/pipermail/stackless/attachments/20080406/2e4b8211/attachment.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PBServer.py
Type: text/x-python
Size: 662 bytes
Desc: 2825570039-PBServer.py
URL: <http://www.stackless.com/pipermail/stackless/attachments/20080406/2e4b8211/attachment-0001.py>

More information about the Stackless mailing list