
本文深入探讨了在同步Python程序中运行异步后台任务的策略。我们首先分析了使用asyncio.create_task而不await时任务无法完成的常见问题,并阐明了asyncio事件循环的工作机制。随后,文章提供了两种核心解决方案:一是在同一事件循环中显式await后台任务以确保其执行,二是通过结合Python的threading模块,在独立的线程中运行asyncio事件循环,从而实现异步任务与同步主程序的并行执行。
Python的asyncio库是用于编写并发代码的强大工具,它基于协程(coroutines)和事件循环(event loop)。然而,理解asyncio的核心概念对于在混合同步/异步环境中正确使用它至关重要。
考虑以下场景:一个同步的main函数希望启动一个异步的后台任务。
import asyncio
from time import sleep
import sys
async def task():
"""模拟一个耗时的后台异步任务"""
for i in range(5):
print(f"Background task iteration {i}")
await asyncio.sleep(1) # 异步等待,让出控制权
print('finished')
async def background_task_starter():
"""负责启动后台任务的异步协程"""
print("a")
asyncio.create_task(task()) # 创建并调度任务,但未等待其完成
print("b")
def main():
"""同步主程序"""
print("Main program started python", sys.version)
asyncio.run(background_task_starter()) # 启动asyncio事件循环并运行background_task_starter
for i in range(3):
sleep(3) # 同步阻塞等待,与asyncio事件循环无关
print(f"Main program iteration {i}")
if __name__ == "__main__":
main()运行上述代码,你可能会得到类似如下的输出:
立即学习“Python免费学习笔记(深入)”;
Main program started python 3.11.6 (main, Oct 23 2023, 22:48:54) [GCC 11.4.0] a b Background task iteration 0 Main program iteration 0 Main program iteration 1 Main program iteration 2
问题解释:
如果你的目标是确保所有异步任务在当前asyncio.run()调用结束前完成,那么你需要显式地await这些任务。
原理: 通过在父协程中对 asyncio.create_task() 返回的 Task 对象进行 await 操作,我们告诉事件循环:在父协程完成之前,必须等待这个子任务完成。这样,事件循环会一直运行,直到所有被 await 的任务都完成。
import asyncio
from time import sleep
import sys
async def task():
"""模拟一个耗时的后台异步任务"""
for i in range(5):
print(f"Background task iteration {i}")
await asyncio.sleep(0.1) # 缩短睡眠时间以便快速演示
print('finished')
async def background_task_starter():
"""负责启动后台任务的异步协程,并等待其完成"""
print("a")
scheduled_task = asyncio.create_task(task()) # 创建任务
print("task scheduled")
# 可以在此处执行其他非阻塞的异步操作
await scheduled_task # 显式等待后台任务完成
print("b")
def main():
"""同步主程序"""
print("Main program started python", sys.version)
asyncio.run(background_task_starter()) # 运行主协程,它会等待子任务
for i in range(3):
sleep(0.5) # 此处的sleep在asyncio事件循环结束后执行
print(f"Main program iteration {i}")
if __name__ == "__main__":
main()输出:
Main program started python 3.11.6 (...) a task scheduled Background task iteration 0 Background task iteration 1 Background task iteration 2 Background task iteration 3 Background task iteration 4 finished b Main program iteration 0 Main program iteration 1 Main program iteration 2
说明: 在此方案中,asyncio.run(background_task_starter()) 会一直运行事件循环,直到 background_task_starter 及其 await 的 scheduled_task 全部完成。这确保了 task 协程能够完整执行。需要注意的是,这种方式下,main 函数中 asyncio.run 之后的代码会在所有异步任务完成后才开始执行,因此并非真正意义上的“后台并行”,而更像是“顺序执行异步任务,然后执行同步任务”。
当你的主应用程序是纯同步的,并且你希望异步任务在不阻塞主程序的情况下并行运行时,可以利用Python的threading模块。在一个单独的线程中启动一个asyncio事件循环,并在该事件循环中运行异步任务。
原理: threading模块允许程序创建多个执行流(线程),这些线程可以并发地执行代码。通过在一个独立的线程中启动 asyncio 事件循环,我们可以让同步的主线程和包含异步事件循环的后台线程同时运行,从而实现真正的并行。
import asyncio
from time import sleep
import sys
import threading
async def task():
"""模拟一个耗时的后台异步任务"""
for i in range(5):
print(f"Background task iteration {i}")
await asyncio.sleep(1) # 保持原有的睡眠时间
print('finished')
async def background_task_runner():
"""作为新线程中asyncio事件循环的入口协程"""
print("a")
await task() # 直接等待task协程完成
print("b")
def run_async_in_thread():
"""在新线程中启动一个独立的asyncio事件循环"""
asyncio.run(background_task_runner())
def main():
"""同步主程序"""
print("Main program started python", sys.version)
# 创建一个新线程来运行asyncio事件循环
t = threading.Thread(target=run_async_in_thread)
t.start() # 启动后台线程
for i in range(3):
sleep(3) # 主程序继续执行其同步任务
print(f"Main program iteration {i}")
# 可以选择等待后台线程完成,确保所有任务都执行完毕
t.join()
print("Main program finished")
if __name__ == "__main__":
main()输出:
Main program started python 3.11.6 (...) a Background task iteration 0 Main program iteration 0 Background task iteration 1 Background task iteration 2 Background task iteration 3 Main program iteration 1 Background task iteration 4 finished b Main program iteration 2 Main program finished
说明: 在此方案中,main 函数通过创建一个新线程 t 来运行 asyncio 事件循环。这样,主线程可以继续执行其同步的 sleep 循环,而后台线程则独立地执行异步的 task 协程。输出
以上就是在同步Python应用中高效运行异步后台任务:asyncio与线程的结合策略的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号