Tornado asynchronous web request client example

2019-09-0100:18:17 Comment

Tornado is a python web Server, which is better for solving long connection request or IO asynchronous problems.

Today, We give a good asynchronous request client code sample ( with Python 2.7.x ). It shows the tornado server request other web source does not block the tornado progress.

import urllib
import traceback
from tornado import gen
from tornado.httpclient import HTTPError
from tornado.httpclient import AsyncHTTPClient, HTTPRequest
AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient")


class AsynchronousTimeout(Exception):
    message = "async request timeout"


class AsyncRequestClient(object):

    client = AsyncHTTPClient(max_clients=40)

    @classmethod
    @gen.coroutine
    def get(cls, url, **headers):
        try:
            req = HTTPRequest(url=url, headers=headers, connect_timeout=5, request_timeout=5)
            response = yield cls.client.fetch(req)
            if response.request_time > 2:
                print("Get url: %s with time: %s" % (url, response.request_time))
        except gen.TimeoutError as e:
            raise AsynchronousTimeout()
        except HTTPError as e:
            raise
        except Exception as e:
            raise AsynchronousTimeout()
        raise gen.Return(response)

    @classmethod
    @gen.coroutine
    def post(cls, url, data, **headers):
        try:
            if isinstance(data, dict):
                data = urllib.urlencode(data)
            req = HTTPRequest(url=url, method="POST", body=data, headers=headers, connect_timeout=20, request_timeout=20)
            response = yield cls.client.fetch(req)
            if response.request_time > 2:
                print("Post url: %s with time: %s" % (url, response.request_time))
        except gen.TimeoutError as e:
            raise AsynchronousTimeout()
        except HTTPError as e:
            raise
        except Exception as e:
            raise AsynchronousTimeout()
        raise gen.Return(response)

    def session(self, url, data, **headers):
        pass

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: