Created
August 12, 2013 17:14
-
-
Save dongweiming/6212964 to your computer and use it in GitHub Desktop.
tornado-async-contrast
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
#!/bin/env python | |
import tornado.httpserver | |
import tornado.ioloop | |
import tornado.options | |
import tornado.web | |
import tornado.httpclient | |
import time | |
from tornado.options import define, options | |
define("port", default=8000, help="run on the given port", type=int) | |
class SleepHandler(tornado.web.RequestHandler): | |
def get(self): | |
time.sleep(5) | |
self.write("when i sleep 5s") | |
class JustNowHandler(tornado.web.RequestHandler): | |
def get(self): | |
self.write("i hope just now see you") | |
if __name__ == "__main__": | |
tornado.options.parse_command_line() | |
app = tornado.web.Application(handlers=[ | |
(r"/sleep", SleepHandler), (r"/justnow", JustNowHandler)]) | |
http_server = tornado.httpserver.HTTPServer(app) | |
http_server.listen(options.port) | |
tornado.ioloop.IOLoop.instance().start() |
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
#!/bin/env python | |
import tornado.httpserver | |
import tornado.ioloop | |
import tornado.options | |
import tornado.web | |
import tornado.gen | |
import tornado.httpclient | |
import time | |
import requests | |
from tornado.options import define, options | |
define("port", default=8000, help="run on the given port", type=int) | |
class SleepHandler(tornado.web.RequestHandler): | |
@tornado.web.asynchronous | |
@tornado.gen.coroutine | |
def get(self): | |
# tornado.gen.Task(time.sleep, 5) | |
http_client = tornado.httpclient.AsyncHTTPClient() | |
response = yield http_client.fetch("http://www.sina.com") | |
self.write("when i sleep 5s") | |
class JustNowHandler(tornado.web.RequestHandler): | |
def get(self): | |
self.write("i hope just now see you") | |
if __name__ == "__main__": | |
tornado.options.parse_command_line() | |
app = tornado.web.Application(handlers=[ | |
(r"/sleep", SleepHandler), (r"/justnow", JustNowHandler)]) | |
http_server = tornado.httpserver.HTTPServer(app) | |
http_server.listen(options.port) | |
tornado.ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
def test(t, callback=None, **kwargs):
future = Future()
future.set_result(time.sleep(t))
return future