gpt4 book ai didi

python - 协程进度的 Asyncio 打印状态

转载 作者:太空宇宙 更新时间:2023-11-03 11:01:29 27 4
gpt4 key购买 nike

我有一堆协程在做一些工作

@asyncio.coroutine
def do_work():
global COUNTER
result = ...
if result.status == 'OK':
COUNTER += 1

还有一个

COUNTER = 0
@asyncio.coroutine
def display_status():
while True:
print(COUNTER)
yield from asyncio.sleep(1)

它必须显示有多少协程已经完成了他们的工作。如何正确实现这项任务?以下解决方案不起作用

@asyncio.coroutine
def spawn_jobs():
coros = []
for i in range(10):
coros.append(asyncio.Task(do_work()))
yield from asyncio.gather(*coros)

if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.create_task(display_status())
loop.run_until_complete(spawn_jobs())
loop.close()

我希望无论 do_work() 协程做什么,计数器都会每秒打印到控制台。但我只有两个输出:0 和几秒钟后重复 10。

最佳答案

But I have just two outputs: 0 and after a few seconds repeating 10.

我无法复制它。如果我使用:

import asyncio
import random

@asyncio.coroutine
def do_work():
global COUNTER
yield from asyncio.sleep(random.randint(1, 5))
COUNTER += 1

我得到这样的输出:

0
0
4
6
8
Task was destroyed but it is pending!
task: <Task pending coro=<display_status() running at most_wanted.py:16> wait_for=<Future pending cb=[Task._wakeup()] created at ../Lib/asyncio/tasks.py:490> created at most_wanted.py:27>

display_status() 中的无限循环导致最后出现警告。避免警告;完成批处理中的所有任务后退出循环:

#!/usr/bin/env python3
import asyncio
import random
from contextlib import closing
from itertools import cycle

class Batch:
def __init__(self, n):
self.total = n
self.done = 0

async def run(self):
await asyncio.wait([batch.do_work() for _ in range(batch.total)])

def running(self):
return self.done < self.total

async def do_work(self):
await asyncio.sleep(random.randint(1, 5)) # do some work here
self.done += 1

async def display_status(self):
while self.running():
await asyncio.sleep(1)
print('\rdone:', self.done)

async def display_spinner(self, char=cycle('/|\-')):
while self.running():
print('\r' + next(char), flush=True, end='')
await asyncio.sleep(.3)

with closing(asyncio.get_event_loop()) as loop:
batch = Batch(10)
loop.run_until_complete(asyncio.wait([
batch.run(), batch.display_status(), batch.display_spinner()]))

输出

done: 0
done: 2
done: 3
done: 4
done: 10

关于python - 协程进度的 Asyncio 打印状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31342858/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com