在我的应用程序中,我有多个类可能会导致将事件添加到event_loop。但是我不希望多次添加协程,所以我需要一种方法来检查协程是否已经是运行循环中的任务。
我找到了 asyncio.Task.all_tasks(loop)类方法。我认为这将解决我的问题,但输出只提供协程名称而不是参数。这对我来说是个问题,因为我在event_loop中添加了带有不同参数集的相同协程。
演示:
import asyncio
from threading import Thread
from time import sleep
async def foo(a, b):
while True:
print("{}{}".format(a, b))
await asyncio.sleep(1)
def loop_starter():
loop1.create_task(foo("one","two"))
loop1.create_task(foo("three","four"))
loop1.run_forever()
loop1 = asyncio.new_event_loop()
t = Thread(target=loop_starter)
t.start()
sleep(0.5)
print(asyncio.Task.all_tasks(loop1))
all_tasks()方法的输出是:
{<Task pending coro=<foo() running at example.py:8> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f7dbb312b58>()]>>,
<Task pending coro=<foo() running at example.py:8> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f7dbb2a4918>()]>>}
所以看来我无法知道事件循环中foo的哪个版本。
答案 0 :(得分:2)
只有想到的东西是这样的:
import asyncio
_running = list() # set would be better if you have hashable values
def create_unique_task(coro_func, *args, **kwargs):
loop = asyncio.get_event_loop()
uid = (args, kwargs)
if uid not in _running:
task = loop.create_task(foo(*args, **kwargs))
_running.append(uid)
task.add_done_callback(lambda _: _running.remove(uid))
else:
print('skipped')
return
return task
async def foo(a, b):
print("{} {}".format(a, b))
await asyncio.sleep(0.5)
async def main():
create_unique_task(foo, "one", "two")
create_unique_task(foo, "three", "four")
await asyncio.sleep(0.2)
create_unique_task(foo, "one", "two")
create_unique_task(foo, "three", "four")
await asyncio.sleep(0.4)
create_unique_task(foo, "one", "two")
create_unique_task(foo, "three", "four")
await asyncio.sleep(1)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(main())
finally:
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
<强>输出:强>
one two
three four
skipped
skipped
one two
three four