
本文旨在解决在 python 中启动异步协程时遇到的困惑,并提供一种在不阻塞主线程的情况下,类似 javascript 的方式立即执行异步任务的方案。文章深入探讨了 `asyncio` 库的特性,并结合 `run_coroutine_threadsafe` 方法展示了如何在独立的事件循环中运行协程,从而实现异步任务的并行执行和状态监控。
在 Python 的异步编程中,一个常见的困惑是如何立即启动一个异步协程,而无需立即 `await` 它。与 JavaScript 等语言不同,Python 的 `async` 函数在调用时并不会立即执行,而是返回一个协程对象。只有通过 `await`、`asyncio.gather` 等方式,协程才会真正开始执行。 这种行为可能导致一些问题,尤其是在需要尽快启动多个协程,并在稍后等待它们完成的情况下。如果使用 `asyncio.gather`,所有协程会在同一时刻启动,导致潜在的性能瓶颈。 为了解决这个问题,我们可以利用 `asyncio.run_coroutine_threadsafe` 函数,在一个独立的线程中运行协程。这允许协程立即启动,而不会阻塞主线程。 **使用 `run_coroutine_threadsafe`** `asyncio.run_coroutine_threadsafe(coro, loop)` 函数接受一个协程对象 `coro` 和一个事件循环 `loop` 作为参数。它将协程提交到指定的事件循环中,并在该循环中运行协程。该函数返回一个 `concurrent.futures.Future` 对象,可以用来获取协程的结果或检查其状态。 以下是一个示例代码,展示了如何使用 `run_coroutine_threadsafe` 函数: ```python import asyncio import time from threading import Thread global_loop = None def thread_for_event_loop(): global global_loop global_loop = asyncio.new_event_loop() asyncio.set_event_loop(global_loop) global_loop.run_forever() t = Thread(target=thread_for_event_loop) t.daemon = True t.start() time.sleep(1) # wait for thread to start old_print = print print = lambda *_: old_print(round(time.perf_counter(), 1), *_) def attempt(future): # doesn't actually do anything, only prints if task is done print(future.done()) async def work(): print("SETUP") await asyncio.sleep(2) print("MIDDLE") await asyncio.sleep(2) print("END") return "Result" async def main(): print("START", int(time.perf_counter())) task = asyncio.run_coroutine_threadsafe(work(), global_loop) attempt(task) attempt(task) print("before first sleep") time.sleep(3) print("after first sleep") attempt(task) attempt(task) print("before second sleep") time.sleep(3) # Block CPU to wait for second sleeping to finish print("after second sleep") attempt(task) attempt(task) print(await asyncio.wrap_future(task)) asyncio.run(main())代码解释:
运行结果:
1.1 START 1 1.1 False 1.1 False 1.1 before first sleep 1.1 SETUP 3.1 MIDDLE 4.1 after first sleep 4.1 False 4.1 False 4.1 before second sleep 5.1 END 7.1 after second sleep 7.1 True 7.1 True 7.1 Result
注意事项:
总结:
立即学习“Python免费学习笔记(深入)”;
通过使用 asyncio.run_coroutine_threadsafe 函数,我们可以实现类似 JavaScript 的异步编程模型,即立即启动异步任务,并在稍后等待它们完成。这种方法可以提高程序的性能和响应速度,尤其是在需要同时运行多个异步任务的情况下。
以上就是在 Python 中无需等待即可启动或恢复异步方法/协程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号