2016-08-01 236 views
0

我知道有很多关于grequests的帖子,比如Asynchronous Requests with Python requests ,它描述了grequests的基本用法,以及如何通过grequests.get()发送钩子。我从这个链接中提取了这段代码。通过grequests调用函数

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个链接我希望可以将输出为

print_test 
print_test 
print_test 
print_test 

我一直在寻找周围和天堂”能找到缺乏输出的原因我很有趣,有一些我缺少的关键信息。

回答

1

我需要检查的来源还没有,但如果你重写你的钩子函数作为

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

它把输出。所以它可能没有给你打电话给原始的钩子(因为它传递的参数比你接受的还要多),并且捕获异常,所以你没有输出

+0

非常感谢你的回应只是试了一下,它的作品完美!我相信你对争论的数量是正确的。 – user3267256

+0

印刷kwargs。结果库将大量关键字参数传递给你的钩子函数。 '{'代理':OrderedDict(),'超时':None,'stream':False,'cert':None,'verify':True}' –