异步请求与Python请求 [英] Asynchronous Requests with Python requests

查看:361
本文介绍了异步请求与Python请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过请求库为Python的文档中提供的示例:

I tried the sample provided within the documentation of the requests library for python:

<一个href=\"http://docs.python-requests.org/en/latest/user/advanced/#asynchronous-requests\">http://docs.python-requests.org/en/latest/user/advanced/#asynchronous-requests

async.map(RS)我得到的回应codeS,但我想请求每一页的内容。

with async.map(rs) I get the response codes but I want to get the content of each page requested.

out = async.map(rs)
print out[0].content

例如只是不工作。

推荐答案

下面的答案是的的适用于请求v0.13.0 +。异步功能被转移到 grequests 这个问题写了。但是,你可以只更换与 grequests 下面请求,它应该工作。

Note

The below answer is not applicable to requests v0.13.0+. The asynchronous functionality was moved to grequests after this question was written. However, you could just replace requests with grequests below and it should work.

我已经离开这个答案是反映原来的问题这是关于使用请求&LT; v0.13.0。

I've left this answer as is to reflect the original question which was about using requests < v0.13.0.

async.map 多个任务的异步的,你必须:

To do multiple tasks with async.map asynchronously you have to:


  1. 定义函数,要与每个对象做什么(你的任务)

  2. 添加该功能作为一个事件挂钩在您的要求

  3. 呼叫 async.map 所有的请求/操作的列表上

  1. Define a function for what you want to do with each object (your task)
  2. Add that function as an event hook in your request
  3. Call async.map on a list of all the requests / actions

例如:

from requests import async
# If using requests > v0.13.0, use
# from grequests import async

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 response.url

# A list to hold our things to do via async
async_list = []

for u in urls:
    # The "hooks = {..." part is where you define what you want to do
    # 
    # Note the lack of parentheses following do_something, this is
    # because the response will be used as the first argument automatically
    action_item = async.get(u, hooks = {'response' : do_something})

    # Add the task to our list of things to do via async
    async_list.append(action_item)

# Do our list of things to do via async
async.map(async_list)

这篇关于异步请求与Python请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆