我正在尝试集成一个tqdm进度条来监视aiohttp在Python 3.5中生成的POST请求.我有一个工作进度条,但似乎无法使用收集结果as_completed().指针感激不尽.
我发现的示例建议使用以下模式,该模式与Python 3.5 async def定义不兼容:
for f in tqdm.tqdm(asyncio.as_completed(tasks), total=len(coros)):
yield from f
Run Code Online (Sandbox Code Playgroud)
在没有进度条的情况下工作(虽然部分编辑)异步代码:
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)
Run Code Online (Sandbox Code Playgroud)
这是我修改的失败尝试loop():
async def …Run Code Online (Sandbox Code Playgroud) 我在 Jupyter 笔记本中使用 tqdm 进行异步操作——我应该使用tqdm.notebook(我认为这能让我得到正确的小部件)还是tqdm.asyncio(这可能会让我得到正确的行为——items/sec calc 在我的使用中似乎关闭了) 。