栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

带有Python请求的异步请求

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

带有Python请求的异步请求

注意
下面的答案是不适用于请求v0.13.0 +。编写此问题后,异步功能已移至

grequests
。但是,你可以将其替换
requests
grequests
下面的内容,它应该可以工作。

我已经留下了这个答案,以反映原始问题,即有关使用请求

<v0.13.0
的问题。

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

  1. 为每个对象定义一个函数(你的任务)
  2. 将该功能添加为请求中的事件挂钩
  3. 调用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)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/575011.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号