Skip to main content
 首页 » 编程设计

Python 异步网络服务调用

2025年05月04日10lvdongjie

我有一个运行 python 代码的树莓派模块。现在我必须从中异步调用 Web 服务。但是当我编写代码并执行时会出现错误

    Traceback (most recent call last): 
  File "async_ws.py", line 3, in <module> 
    from requests import async 
ImportError: cannot import name async 

我的代码是这样的

import json 
import requests 
from requests import async 
 
urls = [ 
    'http://python-requests.org', 
    'http://httpbin.org', 
    'http://python-guide.org', 
    'http://kennethreitz.com' 
] 
 
rs = [async.get(u) for u in urls] 
async.map(rs) 

请帮帮我

请您参考如下方法:

这看起来不像是 python 请求最常见的异步库,它们是 grequests(使用 gevent)和 requests-futures。我会看看你是否可以从 https://github.com/ross/requests-futures 安装请求 future (如果您运行的是 python 2,则包括从 python 3 向后移植的 concurrent.futures)。