繁体   English   中英

Asyncio 在单独的线程中“触发并忘记”任务

[英]Asyncio “fire and forget” tasks in a separate thread

我有一个长时间运行的同步 Python 程序,我想每秒运行约 10 个“即发即弃”任务。 这些任务命中远程 API 并且不需要返回任何值。 我试过这个答案,但它需要太多的 CPU/内存来生成和维护所有单独的线程,所以我一直在研究 asyncio。

这个答案很好地解释了如何使用 asyncio 运行“一劳永逸”。 但是,它需要使用run_until_complete() ,它会等待所有异步任务完成。 我的程序正在使用同步 Python 所以这对我不起作用。 理想情况下,代码应该像这样简单,其中log_remote不会阻塞循环:

while True:
    latest_state, metrics = expensive_function(latest_state)
    log_remote(metrics) # <-- this should be run as "fire and forget"

我在 Python 3.7。 如何在另一个线程上使用 asyncio 轻松运行它?

您可以在单个后台线程中启动单个事件循环,并将其用于您的所有 fire&forget 任务。 例如:

import asyncio, threading

_loop = None

def fire_and_forget(coro):
    global _loop
    if _loop is None:
        _loop = asyncio.new_event_loop()
        threading.Thread(target=_loop.run_forever, daemon=True).start()
    _loop.call_soon_threadsafe(asyncio.create_task, coro)

有了它,您可以在通过调用async def创建的协程fire_and_forget上调用 fire_and_forget :

# fire_and_forget defined as above

import time

async def long_task(msg):
    print(msg)
    await asyncio.sleep(1)
    print('done', msg)

fire_and_forget(long_task('foo'))
fire_and_forget(long_task('bar'))
print('continuing with something else...')
time.sleep(3)

请注意, log_remote实际上需要使用 asyncio、aiohttp 而不是请求等编写为async def

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM