多个 http get 和 post
Multiple http get and 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
我目前是通过requests库来做的,但是一个一个做这个需要很多时间,最多可以达到20000个。
我尝试了 multiprocessing
但由于某种原因它在发送 5000-10000 get 和 post.
后挂起
我读到了有关 grequest 的内容,但上面写着
Order of these responses does not map to the order of the requests you send out.
。我需要订单,因为我必须根据我发送的消息修改每个响应。
这里最好的选择是什么?我也读过有关 threading,tornado
的内容,但由于我搞砸了 multiprocessing
的第一种方法,我想在再次开始之前先确定 [=16] =]
这里有一个解决方案,它允许您使用 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)})
任务是:
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
我目前是通过requests库来做的,但是一个一个做这个需要很多时间,最多可以达到20000个。
我尝试了 multiprocessing
但由于某种原因它在发送 5000-10000 get 和 post.
我读到了有关 grequest 的内容,但上面写着
Order of these responses does not map to the order of the requests you send out.
。我需要订单,因为我必须根据我发送的消息修改每个响应。
这里最好的选择是什么?我也读过有关 threading,tornado
的内容,但由于我搞砸了 multiprocessing
的第一种方法,我想在再次开始之前先确定 [=16] =]
这里有一个解决方案,它允许您使用 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)})