通过 grequests 调用函数

calling functions via grequests

我发现有很多关于 grequests 的帖子,例如 Asynchronous Requests with Python requests 它描述了 grequests 的基本用法以及如何通过 grequests.get() 发送挂钩我从 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)

当我 运行 但是我没有输出

/$ python test.py
/$

因为有 4 个 link,我希望输出为

print_test
print_test
print_test
print_test

我一直在四处寻找,但未能找到缺少输出的原因我很有趣,我遗漏了一些关键信息。

我还需要检查源代码,但是如果您将钩子函数重写为

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

它输出。所以它可能无法调用你原来的钩子(因为它传递的参数比你接受的多)并捕获异常,所以你没有输出