Introduction
Stackless Python is an enhanced version of the Python programming language.
This example shows how to take advantage of Stackless Python with the Twisted framework.
The following example shows how a single method is exposed in this way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import stackless
from twisted.web import resource, http, server, error
from twisted.internet import reactor
from twisted.python import log
from pyamf import remoting
from pyamf.remoting.gateway.twisted import TwistedGateway
from twisted.internet import defer
from twisted.internet import task
class EchoServer(TwistedGateway):
def __init__(self):
super(EchoServer, self).__init__()
self.request = None
return
def __echo__(self, request, deferred, y):
deferred.callback(y)
def echo(self, request, y):
deferred = defer.Deferred()
stackless.tasklet(self.__echo__)(request, deferred, y)
return deferred
if __name__== "__main__":
gw = EchoServer()
gw.addService(gw.echo, "echo", "echo")
root = resource.Resource()
root.putChild('gwplayer', gw)
reactor.listenTCP(8080, server.Site(root))
print "server running on localhost:8080"
task.LoopingCall(stackless.schedule).start(.01)
stackless.tasklet(reactor.run)()
stackless.run()
|