Created
February 10, 2014 08:53
-
-
Save pgk/8912537 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# to start the server: | |
# pip install twisted | |
# pip install cyclone | |
# cyclone run cyclone_ex.py | |
import cyclone.web | |
from twisted.internet import defer, task, reactor | |
@defer.inlineCallbacks | |
def kinda_sleep(t): | |
yield task.deferLater(reactor, t, lambda : True) | |
defer.returnValue(True) | |
class MainHandler(cyclone.web.RequestHandler): | |
@cyclone.web.asynchronous | |
@defer.inlineCallbacks | |
def get(self): | |
for i in xrange(16): | |
stmt = "Hello, world {0}<br />".format(i) | |
self.write(stmt) | |
# flush buffer to network so (partial) response goes to client | |
self.flush() | |
yield kinda_sleep(0.5) | |
self.finish("Bye!<br />") | |
class Application(cyclone.web.Application): | |
def __init__(self): | |
handlers = [ | |
(r"/", MainHandler), | |
] | |
settings = dict( | |
xheaders=False, | |
static_path="./static", | |
templates_path="./templates", | |
) | |
cyclone.web.Application.__init__(self, handlers, **settings) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment