【问题标题】:Multiple http get and post多个http get和post
【发布时间】:2016-11-28 05:53:22
【问题描述】:

任务是:

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 次获取和发布后挂起。

我读到了 grequest,但上面写着 Order of these responses does not map to the order of the requests you send out..我需要订单,因为我必须根据我发送的 get 修改每个回复。

这里最好的选择是什么?我也读过关于 threading,tornado 的文章,但是由于我用 multiprocessing 搞砸了我的第一个方法,我想在再次开始之前确定一下

【问题讨论】:

  • 您在哪里看到响应的顺序与请求的顺序不匹配?我在 GitHub 页面上没有看到这一点,this 与此相矛盾。编辑:没关系,我看到 imap 不会以相同的顺序返回它们。
  • @PeteyPii 这就是我不确定的...是否也仅适用于imapmap。因为最后写的是The API for imap is equivalent to the API for map.
  • 查看源代码似乎向我暗示地图确实与请求保持相同的顺序。

标签: python post get grequests


【解决方案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)})

【讨论】:

  • 所以这不会维持顺序,但允许处理每个响应并对其进行修改,对吗?
  • 是的,它应该是这样工作的(我自己没有尝试过)
猜你喜欢
  • 1970-01-01
  • 2011-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多