etayluz
etayluz

Reputation: 16436

Tornado: pass more arguments to response callback

I'm using tornado to make some async HTTP requests. as such:

from tornado.httpclient import AsyncHTTPClient

AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient", max_clients=10000)
HTTP_CLIENT = AsyncHTTPClient()
HTTP_CLIENT.fetch(request, handle_response)

def handle_response(response):
    """Handle response"""

My questions is how to pass another variable (argument) to handle_response? Something like this (but NOT this):

HTTP_CLIENT.fetch(request, handle_response, some_variable)

def handle_response(response, some_variable):
    """Handle response"""

Upvotes: 1

Views: 455

Answers (1)

A. Jesse Jiryu Davis
A. Jesse Jiryu Davis

Reputation: 24007

Use a "partial":

from functools import partial

HTTP_CLIENT.fetch(request, partial(handle_response, some_variable))


def handle_response(some_variable, response):
    """Handle response"""

Note that "some_variable" is now first, before "response".

Upvotes: 2

Related Questions