Skip to content

Instantly share code, notes, and snippets.

@dongweiming
Created August 12, 2013 17:14
Show Gist options
  • Save dongweiming/6212964 to your computer and use it in GitHub Desktop.
Save dongweiming/6212964 to your computer and use it in GitHub Desktop.
tornado-async-contrast
#!/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()
#!/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()
@dongweiming
Copy link
Author

def test(t, callback=None, **kwargs):
future = Future()
future.set_result(time.sleep(t))
return future

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment