python requests 异步调用_带有Python请求的异步请求
小編典典
注意
下面的答案是不適用于請(qǐng)求v0.13.0 +。編寫(xiě)此問(wèn)題后,異步功能已移至grequests。但是,你可以將其替換requests為grequests下面的內(nèi)容,它應(yīng)該可以工作。
我已經(jīng)留下了這個(gè)答案,以反映原始問(wèn)題,即有關(guān)使用請(qǐng)求
要async.map異步執(zhí)行多個(gè)任務(wù),你必須:
為每個(gè)對(duì)象定義一個(gè)函數(shù)(你的任務(wù))
將該功能添加為請(qǐng)求中的事件掛鉤
調(diào)用async.map所有請(qǐng)求/操作的列表
例:
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)
2020-02-17
總結(jié)
以上是生活随笔為你收集整理的python requests 异步调用_带有Python请求的异步请求的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python上机报告出现的问题和解决方法
- 下一篇: python查询结果写入excel_py