[Stackless] Stackless + Twisted with deferred operations
Carlos Eduardo de Paula
carlosedp at gmail.com
Mon Mar 12 22:08:30 CET 2007
For some time I was thinking about the integration of stackless with
twisted. Greg Hazel's examples opened my eyes to a simple and
effective way of doing the integration.
With this, I ported my producer-consumer application to networked
version where I have the stack in a server and the clients spread
around in a network. to my point of view this approach can have
multiple use cases in real life problems.
Reproducing Greg's code, I used he's nowait channel (NWChannel) and
its accessory code. I did something like this:
reactor.connectTCP("localhost", 8800, factory)
def1 = factory.login(credentials.UsernamePassword(self.login, self.pwd))
def1.addCallback(self.good, self.me, self.ch)
def1.addErrback(self.bad, self.me, self.ch)
self.perspective = self.ch.receive()
where the good and bad callbacks returned the data thru the NWChannel.
Studying Christopher Armstrong's twisted-threadless example, I found
the blockOn function, that can be used to substitute and clean the
code like this:
reactor.connectTCP("localhost", 8800, factory)
def1 = factory.login(credentials.UsernamePassword(self.login, self.pwd))
self.perspective = blockOn(def1)
making the NWChannel, good and bad callbacks not needed anymore.
I changed the blockOn function a bit so it became:
def blockOn(d):
"""
Use me in stacklessy-code to wait for a Deferred to fire.
If the result is an failure, send the exception via the channel
to be captured by the tasklet.
"""
ch = stackless.channel()
def cbOK(r):
ch.send(r)
def cbNOK(r):
ch.send_exception(r.type, r.value)
d.addCallbacks(cbOK, cbNOK)
return ch.receive()
With this, I can do something like:
try:
reactor.connectTCP("localhost", 8800, factory)
def1 =
factory.login(credentials.UsernamePassword(self.login, self.pwd))
self.perspective = blockOn(def1)
except ConnectionRefusedError, val:
print val
print "No connection could be opened."
I would like to receive some feedback on this changes, if is there
anything that could happen if i do not use the NWChannel proposed by
Greg and if is there any open points to be improved.
Regards,
Carlos
_______________________________________________
Stackless mailing list
Stackless at stackless.com
http://www.stackless.com/mailman/listinfo/stackless
More information about the Stackless
mailing list