user3267256
user3267256

Reputation: 113

calling functions via grequests

I realize there have been many posts on grequests such as Asynchronous Requests with Python requests which describes the basic usage of grequests and how to send hooks via grequests.get() I pulled this bit of code right from that link.

import grequests

urls = [
    'http://python-requests.org',
    'http://httpbin.org',
    'http://python-guide.org',
    'http://kennethreitz.com'
]

# A simple task to do to each response object
def do_something(response):
    print ('print_test')

# A list to hold our things to do via async
async_list = []

for u in urls:
    action_item = grequests.get(u, hooks = {'response' : do_something})

    async_list.append(action_item)

# Do our list of things to do via async
grequests.map(async_list)

When i run this however i get no output

/$ python test.py
/$

since there are 4 links I would expect the output to be

print_test
print_test
print_test
print_test

I have been searching around and haven't been able to find a reason for the lack of output I am amusing that there is a bit of key information that I am missing.

Upvotes: 0

Views: 1297

Answers (1)

Darth Kotik
Darth Kotik

Reputation: 2351

I need to check sources yet, but if you rewrite your hook function as

# A simple task to do to each response object
def do_something(response, *args, **kwargs):
    print ('print_test')

it puts output. So it's probably failing to call you original hook(because it passes more arguments than you accept) and catching exception, so you get no output

Upvotes: 2

Related Questions