带 tqdm 的 asyncio aiohttp 进度条

     2023-03-13     232

关键词:

【中文标题】带 tqdm 的 asyncio aiohttp 进度条【英文标题】:asyncio aiohttp progress bar with tqdm 【发布时间】:2016-06-18 20:21:04 【问题描述】:

我正在尝试集成tqdm 进度条来监控在 Python 3.5 中使用aiohttp 生成的 POST 请求。我有一个工作进度条,但似乎无法使用as_completed() 收集结果。感激地收到指点。

我发现的示例建议使用以下模式,该模式与 Python 3.5 async def 定义不兼容:

for f in tqdm.tqdm(asyncio.as_completed(tasks), total=len(coros)):
    yield from f

没有进度条的工作(尽管已编辑)异步代码:

def async_classify(records):

    async def fetch(session, name, sequence):
        url = 'https://app.example.com/api/v0/search'
        payload = 'sequence': str(sequence)
        async with session.post(url, data=payload) as response:
            return name, await response.json()

    async def loop():
        auth = aiohttp.BasicAuth(api_key)
        conn = aiohttp.TCPConnector(limit=100)
        with aiohttp.ClientSession(auth=auth, connector=conn) as session:
            tasks = [fetch(session, record.id, record.seq) for record in records]
            responses = await asyncio.gather(*tasks)    
        return OrderedDict(responses)

这是我修改loop()的失败尝试:

async def loop():
    auth = aiohttp.BasicAuth(api_key)
    conn = aiohttp.TCPConnector(limit=100)
    with aiohttp.ClientSession(auth=auth, connector=conn) as session:
        tasks = [fetch(session, record.id, record.seq) for record in records]
        for f in tqdm.tqdm(asyncio.as_completed(tasks), total=len(tasks)):
            await f
        responses = await asyncio.gather(f)
        print(responses)

【问题讨论】:

【参考方案1】:

await f 返回一个单个响应。为什么要将已经完成的Future 传递给asyncio.gather(f) 尚不清楚。

试试:

responses = []
for f in tqdm.tqdm(asyncio.as_completed(tasks), total=len(tasks)):
    responses.append(await f)

Python 3.6 实现PEP 530 -- Asynchronous Comprehensions:

responses = [await f
             for f in tqdm.tqdm(asyncio.as_completed(tasks), total=len(tasks))]

它现在可以在 async def 函数中使用。

【讨论】:

@j-f-sebastien 可爱!谢谢你告诉我

asyncio/aiohttp 不返回响应

】asyncio/aiohttp不返回响应【英文标题】:asyncio/aiohttpnotreturningresponse【发布时间】:2019-08-0617:47:10【问题描述】:我正在尝试通过使用asyncio/aiohttp并行化Web请求来从https://www.officialcharts.com/中抓取一些数据。我实现了链接here中给... 查看详情

asyncio实现aiohttp(代码片段)

#asyncio没有提供http协议的接口aiohttpimportasyncioimportsocketfromurllib.parseimporturlparseasyncdefget_url(url):#通过socket请求htmlurl=urlparse(url)host=url.netlocpath=url.pathifpath=="":path="/"#建立socket连接reader 查看详情

asyncio 网络抓取 101:使用 aiohttp 获取多个 url

】asyncio网络抓取101:使用aiohttp获取多个url【英文标题】:asynciowebscraping101:fetchingmultipleurlswithaiohttp【发布时间】:2016-06-2521:54:45【问题描述】:在较早的问题中,aiohttp的一位作者建议使用Python3.5中的新asyncwith语法来使用fetchmulti... 查看详情

aiohttp

asyncio可实现单线程并发IO操作。如果把asyncio用在服务器端,例如web服务器,由于HTTP连接就是IO操作,因此可以用单线程+协程实现多用户的高并发支持。 asyncio实现了TCP,UDP,SSL等协议,aiohttp则是基于asyncio实现的HTTP框架。编写... 查看详情

使用 aiohttp/asyncio 发出 100 万个请求 - 字面意思

】使用aiohttp/asyncio发出100万个请求-字面意思【英文标题】:Making1milionrequestswithaiohttp/asyncio-literally【发布时间】:2016-12-1408:29:10【问题描述】:我跟进了本教程:https://pawelmhm.github.io/asyncio/python/aiohttp/2016/04/22/asyncio-aiohttp.html,当... 查看详情

python使用asyncio和aiohttp的scrapper的一个例子(代码片段)

查看详情

python使用asyncio和aiohttp的scrapper的一个例子(代码片段)

查看详情

无法使用 asyncio/aiohttp 返回 404 响应

】无法使用asyncio/aiohttp返回404响应【英文标题】:Havingtroublereturning404responseswithasyncio/aiohttp【发布时间】:2020-06-0100:21:45【问题描述】:importtimeimportasyncioimportaiohttpasyncdefis_name_available(s,name):asyncwiths.get("https://twitter 查看详情

将 tornado 与 aiohttp(或其他基于 asyncio 的库)一起使用

】将tornado与aiohttp(或其他基于asyncio的库)一起使用【英文标题】:Usingtornadowithaiohttp(orotherasyncio-basedlibraries)【发布时间】:2016-01-1306:30:40【问题描述】:我想将tornado与aiohttp和本机python3.5协程等asyncio库一起使用,最新的tornado... 查看详情

asyncio和aiohttp(代码片段)

asyncio官网https://docs.python.org/zh-cn/3/library/asyncio-task.html下面为伪代码:importaiohttpimportasynciofrombs4importBeautifulSoupimportpandasaspd#将数据存入li=[]或数据库#获取页面li=[]asyncdeffetch(url,session):asyncwi 查看详情

Asyncio 和 aiohttp 将所有 url 路径路由到处理程序

】Asyncio和aiohttp将所有url路径路由到处理程序【英文标题】:Asyncioandaiohttprouteallurlspathstohandler【发布时间】:2016-01-0212:06:49【问题描述】:我很难找到匹配所有传入url的通配符url匹配模式。这只是匹配一个只有主机名的url:import... 查看详情

即使使用 asyncio 和 aiohttp,方法也会等待请求响应

】即使使用asyncio和aiohttp,方法也会等待请求响应【英文标题】:Evenusingasyncioandaiohttp,methodswaitfortherequestresponse【发布时间】:2019-05-2711:34:55【问题描述】:您好我有以下问题,我想执行getlastItemFromGivenInterval方法,让它在不等待... 查看详情

使用 AsyncIO 和 aiohttp 爬取网站并收集所有 url 的程序

】使用AsyncIO和aiohttp爬取网站并收集所有url的程序【英文标题】:AprogramusingAsyncIOandaiohttptocrawlasiteandcollectallurls【发布时间】:2017-06-2013:59:13【问题描述】:使用这种异步(更快)的解决方案,您可以从站点收集所有url,并将它... 查看详情

Django `python manage.py runserver` 不支持 asyncio&aiohttp

】Django`pythonmanage.pyrunserver`不支持asyncio&aiohttp【英文标题】:Django`pythonmanage.pyrunserver`doesnotsupportasyncio&aiohttp【发布时间】:2018-12-2601:52:49【问题描述】:在我的Django应用程序中,我需要将来自用户的请求代理到其他服务器... 查看详情

aiohttp/asyncio多次请求

#!/usr/bin/envpython#-*-coding:utf-8-*-__author__="DanielAltiparmak([email protected])"__copyright__="Copyright(C)2015DanielAltiparmak"__license__="GPL3.0"importasyncioimportaiohttpimporttqdmimpo 查看详情

python使用python的aiohttp和asyncio进行多个异步httpget请求(代码片段)

查看详情

我如何*正确*在循环中运行 asyncio/aiohttp 请求?

】我如何*正确*在循环中运行asyncio/aiohttp请求?【英文标题】:HowdoI*properly*runanasyncio/aiohttprequestinaloop?【发布时间】:2018-04-1704:08:13【问题描述】:我正在尝试同时请求一堆URL,但是这些URL是从列表中构建的。目前我正在遍历列... 查看详情

Python asyncio/aiohttp:ValueError:Windows 上 select() 中的文件描述符过多

】Pythonasyncio/aiohttp:ValueError:Windows上select()中的文件描述符过多【英文标题】:Pythonasyncio/aiohttp:ValueError:toomanyfiledescriptorsinselect()onWindows【发布时间】:2017-12-0613:23:13【问题描述】:注意:未来的读者请注意,这个问题很老,格... 查看详情