Skip to content

Instantly share code, notes, and snippets.

@pgk
Created February 10, 2014 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgk/8912537 to your computer and use it in GitHub Desktop.
Save pgk/8912537 to your computer and use it in GitHub Desktop.
# 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