我在 Jupyter 笔记本中使用 tqdm 进行异步操作——我应该使用tqdm.notebook(我认为这能让我得到正确的小部件)还是tqdm.asyncio(这可能会让我得到正确的行为——items/sec calc 在我的使用中似乎关闭了) 。
小智 1
您可以通过使用此 stackoverflow 线程中的解决方案,在 jupyter 笔记本中使用 asyncio 兼容版本的 tqdm :
并将您使用的 tqdm 替换为笔记本版本:
from tqdm.notebook import tqdm
async def factorial(name, number):
f = 1
for i in range(2, number + 1):
await asyncio.sleep(random.random())
f *= i
return f"Task {name}: factorial {number} = {f}"
requests = [factorial("A", 2), factorial("B", 3), factorial("C", 4)]
responses = [await f for f in tqdm(asyncio.as_completed(requests), total=len(requests))]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
721 次 |
| 最近记录: |