How to make a wrapper that simplifies requests with tornado AsyncHTTPClient?

Kenet Jervet

I want to write a simple function that releases me from repeatedly creating AsyncHTTPClient and HTTPRequest and doing client.fetch.

Now I came up with the following code:

def async_fetch(url, method='GET', headers={}, body=None):
    '''Fetch an HTTP resource asynchronously
       using `tornado.httpclient.AsyncHTTPClient`
    '''

    if isinstance(body, dict):
        body = urlencode(body)

    request = HTTPRequest(url, method=method, headers=headers, body=body)
    client = AsyncHTTPClient()
    response = yield client.fetch(request)
    return response.body

Obviously this will not work. It returns a generator itself and I don't know how to apply it in a RequestHandler.

I can merely return client.fetch(client), which is a task, and yield this task in the handler, but I want the processing of the response wrapped out as well.

Any hints? Thanks!

Vladimir

If you're using tornado >=3 you may create custom coroutine that returns something using gen.Return:

@gen.coroutine
def async_fetch(url, method='GET', headers={}, body=None):
    async_http_client = AsyncHTTPClient()
    http_request = HTTPRequest(url, method=method, headers=headers, body=body)
    http_response = yield client.fetch(http_request)
    raise gen.Return(http_response)  # That's how return looks in tornado

class MyHandler(RequestHandler):
    @gen.coroutine
    def get():
        ...
        response = yield async_fetch('http://example.org')
        ...

In tornado 2.x a more wordly approach is required:

@gen.engine
def async_fetch(url, method='GET', headers={}, body=None, callback=None):
    async_http_client = AsyncHTTPClient()
    http_request = HTTPRequest(url, method=method, headers=headers, body=body)
    http_response = yield client.fetch(http_request)
    if callback:
        callback(http_response)

class MyHandler(RequestHandler):
    @web.asynchronous
    @gen.engine
    def get():
        ...
        response = yield gen.Task(async_fetch, 'http://example.org')
        ...

You may go even further and return an instance of gen.Task from async_fetch.

Update Both gen.coroutine and gen.Return are valuable. gen.coroutine allows to call one coroutine from another (yield client.fetch(..)). gen.Return allows to return some value from generator. Both were introduced to overcome Python <3.4 limitations -- the lack of yield from construction.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Python tornado AsyncHTTPClient 599

分類Dev

TornadoのAsyncHTTPClientサブクラス

分類Dev

How to make repeated requests for tasks

分類Dev

How to make two Python Tornado websocket channels available at different URLs

分類Dev

How to make asynchronous HTTP requests in PHP

分類Dev

How to make asynchronous HTTP requests in PHP

分類Dev

How to make a property required on POST but not on PUT requests

分類Dev

How to make API requests with intervals with NodeJS

分類Dev

How to make HTTP requests using Plink

分類Dev

How do I make my ImmutableList sortable in its ObservableList wrapper?

分類Dev

why is tornado in python blocking my requests

分類Dev

Make a wrapper for cout?

分類Dev

How to Authenticate WebSockets in Tornado

分類Dev

How to make Node.js handle multiple requests efficiently?

分類Dev

How to make all binary files (images) download using requests() and open()?

分類Dev

How to make a Blazor webserver app listen to a POST requests in an endpoint?

分類Dev

How to make multiple parallel concurrent requests with Rails and Heroku

分類Dev

Tornado AsyncHTTPClientは永続化接続をサポートしていますか?

分類Dev

How to do an "internal redirect" in Tornado?

分類Dev

A thin wrapper of ACE editor to make a React Component

分類Dev

Using tor to make requests python?

分類Dev

How to make Angular 2 send all requests as application/x-www-form-urlencoded

分類Dev

how to make angular app using spring boot backend receive only requests from local area network?

分類Dev

How to asynchronously make HTTP Requests in AWS Lambda Node.js runtime

分類Dev

How to get form POST input in Tornado?

分類Dev

How to allow ws:// instead of localhost:// for tornado

分類Dev

How to deserialize array into wrapper object?

分類Dev

How to redirect all HTTPS requests to HTTP requests?

分類Dev

tornado.httpclient.AsyncHTTPClientを使用しているときに「RuntimeWarning:enable tracemalloc to get the object割り当てtraceback」を修正するにはどうすればよいですか?

Related 関連記事

  1. 1

    Python tornado AsyncHTTPClient 599

  2. 2

    TornadoのAsyncHTTPClientサブクラス

  3. 3

    How to make repeated requests for tasks

  4. 4

    How to make two Python Tornado websocket channels available at different URLs

  5. 5

    How to make asynchronous HTTP requests in PHP

  6. 6

    How to make asynchronous HTTP requests in PHP

  7. 7

    How to make a property required on POST but not on PUT requests

  8. 8

    How to make API requests with intervals with NodeJS

  9. 9

    How to make HTTP requests using Plink

  10. 10

    How do I make my ImmutableList sortable in its ObservableList wrapper?

  11. 11

    why is tornado in python blocking my requests

  12. 12

    Make a wrapper for cout?

  13. 13

    How to Authenticate WebSockets in Tornado

  14. 14

    How to make Node.js handle multiple requests efficiently?

  15. 15

    How to make all binary files (images) download using requests() and open()?

  16. 16

    How to make a Blazor webserver app listen to a POST requests in an endpoint?

  17. 17

    How to make multiple parallel concurrent requests with Rails and Heroku

  18. 18

    Tornado AsyncHTTPClientは永続化接続をサポートしていますか?

  19. 19

    How to do an "internal redirect" in Tornado?

  20. 20

    A thin wrapper of ACE editor to make a React Component

  21. 21

    Using tor to make requests python?

  22. 22

    How to make Angular 2 send all requests as application/x-www-form-urlencoded

  23. 23

    how to make angular app using spring boot backend receive only requests from local area network?

  24. 24

    How to asynchronously make HTTP Requests in AWS Lambda Node.js runtime

  25. 25

    How to get form POST input in Tornado?

  26. 26

    How to allow ws:// instead of localhost:// for tornado

  27. 27

    How to deserialize array into wrapper object?

  28. 28

    How to redirect all HTTPS requests to HTTP requests?

  29. 29

    tornado.httpclient.AsyncHTTPClientを使用しているときに「RuntimeWarning:enable tracemalloc to get the object割り当てtraceback」を修正するにはどうすればよいですか?

ホットタグ

アーカイブ