2016-11-28 59 views
3

的任務是:我目前正在做這通過請求庫多個HTTP GET和POST

1)send an http get to url based on a parameter 
2)Modify the response based on the same parameter 
3)send an http post to url based on the same parameter 

,但它需要大量的時間做這一個接一個,它可高達20,000。

我試過multiprocessing但由於某種原因,它在發送5000-10000個get和post後掛起。

我讀過關於grequest,但它說有 Order of these responses does not map to the order of the requests you send out.。我需要訂單,因爲我必須根據我發送的get來修改每個響應。

什麼是最好的選擇嗎?我看了一下threading,tornado太多,但我已經搞砸了我的第一種方法與multiprocessing我想再次獲得到它

+0

你在哪裏看到響應順序與請求順序不匹配?我沒有在GitHub頁面上看到這一點,並且[this](http://stackoverflow.com/questions/37502384/are-grequests-responses-in-the-same-order-as-the-requests)與此相矛盾。編輯:沒關係,我看到,imap不會以相同的順序返回它們。 – PeteyPii

+0

@PeteyPii這就是m不能確定的......它只是用於'imap'或'map'。因爲最後它寫的'imap的API相當於map的API .' – vks

+0

看着來源似乎向我暗示地圖確實與請求保持相同的順序。 – PeteyPii

回答

1

這裏之前一定是一個解決方案,讓您使用grequest的imap(理論上它比grequest的map函數更快),並知道一個索引來映射對請求的響應。信貸到question asked on the project's GitHub issues

from functools import partial 

def callback(index, response, **kwargs): 
    response.image_index = index 

rs = [ 
    grequests.get(
     url, 
     callback=partial(callback, index) 
    ) 
    for index, url in enumerate(urls) 
] 

您應該可以根據您的需求量身定做。

編輯: 我用這個成功hooks

grequests.get(
     url,hooks={'response': partial(process_response, index)}) 
+0

所以這不會保持順序,但將允許與每個響應一起工作並修改它的權利? – vks

+0

是的,這就是它應該如何工作的(我自己並沒有嘗試過) – PeteyPii