python requests 异步调用_带有Python请求的异步请求

下面的答案是不适用于请求v0.13.0+。异步功能被移到G请求书写完这个问题之后。但是,您可以直接替换requests带着grequests在下面,它应该能工作。

我留下这个答案是为了反映最初的问题,这个问题是关于使用请求

若要执行多个任务,请执行以下操作async.map 异步你必须:为每个对象定义一个函数(任务)

将该功能添加为请求中的事件钩子。

打电话

async.map在所有请求/操作的列表中

例子:from requests import async# If using requests > v0.13.0, use# from grequests import asyncurls = [

'http://python-requests.org',

'http://httpbin.org',

'http://python-guide.org',

'http://kennethreitz.com']# A simple task to do to each response objectdef do_something(response):

print response.url# A list to hold our things to do via asyncasync_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 asyncasync.map(async_list)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值