Created
November 6, 2009 20:25
-
-
Save sris/228264 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 logging | |
import tornado.ioloop | |
import tornado.httpclient | |
import tornado.options | |
url = "127.0.0.1:8888" | |
def first(): | |
def handle_response(response): | |
logging.info("Response: %s", response.body) | |
second() | |
http = tornado.httpclient.AsyncHTTPClient() | |
logging.info("sending failing request") | |
http.fetch(url + "/?delay=2", | |
request_timeout=1, | |
callback=handle_response) | |
logging.info("sending slow request") | |
http.fetch(url + "/?delay=1000", | |
request_timeout=1000, | |
callback=logging.info) | |
def second(): | |
def handle_response(response): | |
logging.info("Response: %s", response.body) | |
http = tornado.httpclient.AsyncHTTPClient() | |
logging.info("sending second request") | |
http.fetch(url, callback=handle_response) | |
def main(): | |
tornado.options.parse_command_line() | |
tornado.ioloop.IOLoop.instance().add_callback(first) | |
tornado.ioloop.IOLoop.instance().start() | |
if __name__ == "__main__": | |
main() |
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 logging | |
import time | |
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 MainHandler(tornado.web.RequestHandler): | |
@tornado.web.asynchronous | |
def get(self): | |
delay = int(self.get_argument("delay", 0)) | |
logging.info("delaying %d sec", delay) | |
tornado.ioloop.IOLoop.instance().add_timeout(time.time() + delay, | |
self.async_callback(self.send_response)) | |
def send_response(self): | |
self.write("This mission is too important for me to " | |
"allow you to jeopardize it.\n") | |
self.finish() | |
def main(): | |
tornado.options.parse_command_line() | |
logging.info("listening on port %d", options.port) | |
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