Created
November 19, 2009 12:41
-
-
Save sris/238741 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import time | |
import logging | |
import functools | |
import tornado.httpserver | |
import tornado.ioloop | |
import tornado.options | |
import tornado.web | |
from tornado.options import define, options | |
define("port", default=8888, help="run on the given port", type=int) | |
class Longrunner(object): | |
def do_later(self, evilbit, on_success, on_error): | |
logging.info("doing application stuff...") | |
if evilbit: | |
on_error("oops!") | |
else: | |
on_success("ok!") | |
logging.info("doing some application clean up...") | |
class MainHandler(tornado.web.RequestHandler): | |
@tornado.web.asynchronous | |
def get(self): | |
delay = int(self.get_argument("delay", 0)) | |
error = self.get_argument("error", None) | |
longrunner = functools.partial(Longrunner().do_later, | |
error, | |
self.async_callback(self.on_success), | |
self.async_callback(self.on_error)) | |
tornado.ioloop.IOLoop.instance().add_timeout(time.time() + delay, | |
longrunner) | |
def on_success(self, data): | |
self.write(data) | |
self.finish() | |
def on_error(self, data): | |
raise tornado.web.HTTPError(400) | |
def main(): | |
tornado.options.parse_command_line() | |
application = tornado.web.Application([ | |
(r"/", MainHandler), | |
]) | |
http_server = tornado.httpserver.HTTPServer(application) | |
http_server.listen(options.port) | |
tornado.ioloop.IOLoop.instance().start() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment