25. 使用 Asyncio 的常见反对意见¶
25. Common Objections to Using Asyncio
异步和协程可能不是解决程序中所有并发问题的最佳解决方案。
话虽如此,也可能存在一些误解,阻碍您充分、最佳地利用 Python 中 asyncio 的功能。
在本节中,我们将回顾开发人员在考虑使用 asyncio 时遇到的一些常见反对意见。
Asyncio and coroutines may not be the best solution for all concurrency problems in your program.
That being said, there may also be some misunderstandings that are preventing you from making full and best use of the capabilities of the asyncio in Python.
In this section, we review some of the common objections seen by developers when considering using the asyncio.
25.1 全局解释器锁 (GIL) 怎么样?¶
25.1 What About the Global Interpreter Lock (GIL)?
GIL 保护 Python 解释器的内部免受多个线程的并发访问和修改。
asyncio 事件循环在一个线程中运行。
这意味着所有协程都在单个线程中运行。
因此,使用 asyncio 和协程时,GIL 不是问题。
The GIL protects the internals of the Python interpreter from concurrent access and modification from multiple threads.
The asyncio event loop runs in one thread.
This means that all coroutines run in a single thread.
As such the GIL is not an issue when using asyncio and coroutine.
25.2 Python 协程是“真实的”吗?¶
25.2 Are Python Coroutines “Real“?
协程由软件管理。
协程在 Python 运行时的 asyncio 事件循环中运行和管理(切换)。
它们不是底层操作系统级别提供的功能以及软件表示,例如线程和进程。
从这个意义上说,Python 不支持“原生协程”,但我不确定现代操作系统中是否存在这样的东西。
Coroutines are managed in software.
Coroutines run and are managed (switched) within the asyncio event loop in the Python runtime.
They are not a software representation of a capability provided by the underlying operating system, like threads and processes.
In this sense, Python does not have support for “native coroutines”, but I’m not sure such things exist in modern operating systems.
25.3 Python 的并发性不是有问题吗?¶
25.3 Isn’t Python Concurrency Buggy?
不🙅🏻♀️。
Python 通过协程、线程和进程提供一流的并发性。
它已经存在很长时间了,并且广泛应用于开源和商业项目中。
No.
Python provides first-class concurrency with coroutines, threads, and processes.
It has for a long time now and it is widely used in open source and commercial projects.
25.4 对于并发来说,Python 不是一个糟糕的选择吗?¶
25.4 Isn’t Python a Bad Choice for Concurrency?
开发人员喜爱 Python 的原因有很多,最常见的是因为它易于使用且开发速度快。
Python 通常用于粘合代码、一次性脚本,但越来越多地用于大型软件系统。
如果您使用 Python 并且需要并发性,那么您可以使用现有的东西。 这个问题毫无意义。
如果您需要并发性并且尚未选择一种语言,那么另一种语言可能更合适,也可能不合适。 需要考虑项目的全部功能和非功能需求(或用户的需求、想法和愿望)以及不同开发平台的功能。
Developers love python for many reasons, most commonly because it is easy to use and fast for development.
Python is commonly used for glue code, one-off scripts, but more and more for large-scale software systems.
If you are using Python and then you need concurrency, then you work with what you have. The question is moot.
If you need concurrency and you have not chosen a language, perhaps another language would be more appropriate, or perhaps not. Consider the full scope of functional and non-functional requirements (or user needs, wants, and desires) for your project and the capabilities of different development platforms.
25.5 为什么不使用线程来代替?¶
25.5 Why Not Use Threads Instead?
-
您可以使用线程而不是异步。
-
任何使用线程开发的程序都可以使用 asyncio 和协程重写。
-
任何使用协程和 asyncio 开发的程序都可以使用线程重写。
-
在项目中采用 asyncio 是一种选择,其理由由您决定。
-
在大多数情况下,它们在功能上是等效的。
-
许多用例使用线程将执行得更快,并且可能为更广泛的 Python 开发人员所熟悉。
-
使用 asyncio 时,网络编程和执行系统命令领域的一些用例可能会更简单(最简单)(代码更少),并且比使用线程更具可扩展性。
You can use threads instead of asyncio.
Any program developed using threads can be rewritten to use asyncio and coroutines.
Any program developed using coroutines and asyncio can be rewritten to use threads.
Adopting asyncio in a project is a choice, the rationale is yours.
For the most part, they are functionally equivalent.(功能等效)
Many use cases will execute faster using threads and may be more familiar(亲切) to a wider array of Python developers.
Some use cases in the areas of network programming and executing system commands may be simpler(最简单) (less code) when using asyncio, and significantly more scalable than using threads.
创建日期: 2024年9月4日