Python Asyncio:完整指南
发表于 : 2022年 11月 22日 10:55
来自:https://superfastpython.com/python-asyncio/
Asyncio允许我们在Python中使用基于coroutine并发的异步编程。
尽管Asyncio在Python中已经存在很多年了,但它仍然是Python中最有趣但也是最令人沮丧的领域之一。
对于新的开发者来说,要开始使用asyncio简直是难上加难。
本指南对Python中的asyncio进行了详细而全面的回顾,包括如何定义、创建和运行cououtines,什么是异步编程,什么是非阻塞io,与cououtines一起使用的并发原语,常见问题,以及最佳实践。
这是一个巨大的29000多字的指南。你可能想把它收藏起来,这样你就可以在开发并发程序时参考它。让我们深入了解一下。
Asyncio允许我们在Python中使用基于coroutine并发的异步编程。
尽管Asyncio在Python中已经存在很多年了,但它仍然是Python中最有趣但也是最令人沮丧的领域之一。
对于新的开发者来说,要开始使用asyncio简直是难上加难。
本指南对Python中的asyncio进行了详细而全面的回顾,包括如何定义、创建和运行cououtines,什么是异步编程,什么是非阻塞io,与cououtines一起使用的并发原语,常见问题,以及最佳实践。
这是一个巨大的29000多字的指南。你可能想把它收藏起来,这样你就可以在开发并发程序时参考它。让我们深入了解一下。
- What is Asynchronous Programming
- What is Asyncio
- When to Use Asyncio
- Coroutines in Python
- Define, Create and Run Coroutines
- What is the Event Loop
- Create and Run Asyncio Tasks
- Work With and Query Tasks
- Current and Running Tasks
- Run Many Coroutines Concurrently
- Wait for A Collection of Tasks
- Wait for a Coroutine with a Time Limit
- Shield Tasks from Cancellation
- Run a Blocking Task in Asyncio
- Asynchronous Iterators
- Asynchronous Generators
- Asynchronous Context Managers
- Asynchronous Comprehensions
- Run Commands in Non-Blocking Subprocesses
- Non-Blocking Streams
- Example of Checking Website Status
- Python Asyncio Common Errors
- Python Asyncio Common Questions
- How to Stop a Task?
- How to Wait for a Task To Finish?
- How to Get a Return Value from a Task?
- How to Run a Task in the Background?
- How to Wait for All Background Tasks?
- Does a Running Task Stop the Event Loop from Exiting?
- How to Show Progress of Running Tasks?
- How to Run a Task After a Delay?
- How to Run a Follow-Up Task?
- How to Execute a Blocking I/O or CPU-bound Function in Asyncio?
- Common Objections to Using Asyncio
- Further Reading
- Conclusions