spyoungtech / grequests

Requests + Gevent = <3
https://pypi.python.org/pypi/grequests
BSD 2-Clause "Simplified" License
4.49k stars 331 forks source link

Support for associating request with context data #96

Closed soedjais closed 6 years ago

soedjais commented 8 years ago

What would be the official way of associating a context data (e.g. dictionary, list, etc) to a request in a way so that it can be referred later from a response object? I used to maintain it myself, and then passed an id as a header value, which later retrieved from response.request.header. But that meant the id got sent to destination server. It would be nice if we could have some place to store data related to request but not actually sent anywhere. Thanks!

tdhooper commented 8 years ago

I'm doing this:

def callback(index, response, **kwargs):
    response.image_index = index

rs = [
    grequests.get(
        url,
        timeout=IMG2_TIMEOUT,
        callback=partial(callback, index)
    )
    for index, url in enumerate(urls)
]
soedjais commented 8 years ago

Very smart. Thank you.