2016-08-01 207 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}' –