Search code examples
pythontornado

call internal function by url causes timeout


Because when I try to call a function through the localhost URL, does it cause timeout error?

Exemple code:

import tornado.ioloop
import tornado.web
import requests
import os
from threading import Timer, Thread

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

class TestTornado(tornado.web.RequestHandler):
    def get(self):
        url = "http://localhost:8888"
        requests.get(url, timeout=5)
        self.write("OK!")

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
        (r"/test", TestTornado),
    ])

if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

I try call http://127.0.0.1:8888/test, but this error is generated:

`
Traceback (most recent call last):
  File "/home/mpimentel/envtornado/lib/python3.6/site-packages/tornado/web.py", line 1590, in _execute
    result = method(*self.path_args, **self.path_kwargs)
  File "server.py", line 14, in get
    requests.get(url, timeout=5)
  File "/home/mpimentel/envtornado/lib/python3.6/site-packages/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/home/mpimentel/envtornado/lib/python3.6/site-packages/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/mpimentel/envtornado/lib/python3.6/site-packages/requests/sessions.py", line 524, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/mpimentel/envtornado/lib/python3.6/site-packages/requests/sessions.py", line 637, in send
    r = adapter.send(request, **kwargs)
  File "/home/mpimentel/envtornado/lib/python3.6/site-packages/requests/adapters.py", line 529, in send
    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='localhost', port=8888): Read timed out. (read timeout=5)
`

What would be the correct way to make this call?


Solution

  • requests is a synchronous library, but Tornado is an asynchronous framework. You should not call blocking, synchronous functions like requests.get() from the Tornado IOLoop thread. You must make get() a coroutine and either call requests.get() from a thread pool:

    async def get(self):
        resp = await IOLoop.current().run_in_executor(None, lambda: requests.get(...))
    

    or replace use of requests with a non-blocking equivalent like Tornado's AsyncHTTPClient:

    async def get(self):
        client = tornado.httpclient.AsyncHTTPClient()
        resp = await client.fetch(url, ...)