0

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.

Community
  • 1
  • 1
user3267256
  • 113
  • 2
  • 11

1 Answers1

2

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

Darth Kotik
  • 2,261
  • 1
  • 20
  • 29
  • Awesome thanks for the response just tried it out and it works perfect! I believe you are correct about the amount of arguments. – user3267256 Aug 01 '16 at 20:48
  • printed kwargs. Turned out library passes whole bunch of key-word arguments to your hook function. `{'proxies': OrderedDict(), 'timeout': None, 'stream': False, 'cert': None, 'verify': True}` – Darth Kotik Aug 01 '16 at 20:52