Skip to content

Instantly share code, notes, and snippets.

@jdmaturen
Created January 14, 2011 08:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdmaturen/779349 to your computer and use it in GitHub Desktop.
Save jdmaturen/779349 to your computer and use it in GitHub Desktop.
ms timeout in tornado
diff --git a/tornado/httpclient.py b/tornado/httpclient.py
index 25d07ae..89e1fd4 100644
--- a/tornado/httpclient.py
+++ b/tornado/httpclient.py
@@ -536,7 +536,8 @@ def _curl_setup_request(curl, request, buffer, headers):
curl.setopt(pycurl.FOLLOWLOCATION, request.follow_redirects)
curl.setopt(pycurl.MAXREDIRS, request.max_redirects)
curl.setopt(pycurl.CONNECTTIMEOUT, int(request.connect_timeout))
- curl.setopt(pycurl.TIMEOUT, int(request.request_timeout))
+ curl.setopt(pycurl.NOSIGNAL, 1)
+ curl.setopt(pycurl.TIMEOUT_MS, int(request.request_timeout))
if request.user_agent:
curl.setopt(pycurl.USERAGENT, _utf8(request.user_agent))
else:
(jd@orchid) /home/jd/public_html> time curl http://127.0.0.1:9888/
Init
Hello, world 0ms
http://localhost/~jd/sleep.php?ms=100 103ms
http://localhost/~jd/sleep.php?ms=185 191ms
http://localhost/~jd/sleep.php?ms=600 188ms
HTTP 599: Operation timed out after 227 milliseconds with 0 out of -1 bytes received
real 0m0.222s
user 0m0.001s
sys 0m0.002s
(jd@orchid) /home/jd/public_html> time curl http://127.0.0.1:9888/
Init
Hello, world 0ms
http://localhost/~jd/sleep.php?ms=100 101ms
http://localhost/~jd/sleep.php?ms=185 188ms
http://localhost/~jd/sleep.php?ms=600 188ms
HTTP 599: Operation timed out after 228 milliseconds with 0 out of -1 bytes received
real 0m0.193s
user 0m0.003s
sys 0m0.001s
<?php
$t = !empty($_GET['ms']) ? $_GET['ms'] : 100;
usleep($t * 1000);
echo $t . "\n";
import json
import time
import tornado.httpclient
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
define("port", default=9888, help="run on the given port", type=int)
class MainHandler(tornado.web.RequestHandler):
def initialize(self):
self.write('Init\n')
self.count = 0
self.start_time = time.time()
@tornado.web.asynchronous
def get(self):
self.write("Hello, world %dms\n" % (1000 * (time.time() - self.start_time)))
self.flush()
http = tornado.httpclient.AsyncHTTPClient()
req = tornado.httpclient.HTTPRequest('http://localhost/~jd/sleep.php?ms=100', request_timeout=200)
req2 = tornado.httpclient.HTTPRequest('http://localhost/~jd/sleep.php?ms=600', request_timeout=200)
req3 = tornado.httpclient.HTTPRequest('http://localhost/~jd/sleep.php?ms=185', request_timeout=200)
http.fetch(req3, callback=self.on_response)
http.fetch(req2, callback=self.on_response)
http.fetch(req, callback=self.on_response)
self.count += 3
def on_response(self, response):
self.write('%s %dms\n' % (response.request.url, response.request_time * 1000))
if response.error:
self.write(str(response.error) + '\n')
self.count -= 1
if self.count == 0:
self.finish()
else:
self.flush()
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