In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "what's the difference between Python synchronization and asynchronism", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "what's the difference between Python synchronization and asynchrony" can help you solve your doubts.
What do you mean by "synchronous" and "asynchronous"?
Web applications typically process many requests from different clients in a short period of time. To avoid processing delays, you must consider processing multiple requests in parallel, which is often referred to as concurrency.
In this article, I will continue to use Web applications as examples, but there are other types of applications that also benefit from concurrency. Therefore, this discussion is not limited to Web applications.
The terms "synchronous" and "asynchronous" refer to two ways of writing concurrent applications. So-called "synchronous" servers use threads and processes supported by the underlying operating system to achieve this concurrency.
In this case, we have five clients that all send requests to the application. The access entry to this application is a Web server that acts as a load balancer by assigning services to a server worker pool, which can be implemented as processes, threads, or a combination of both. These worker execute the requests assigned to them by the load balancer. The application logic you write using Web application frameworks (such as Flask or Django) runs in these worker.
This type of solution is better for servers with multiple CPU, because you can set the number of worker to the number of CPU, so you can make balanced use of your processor core, which cannot be achieved by a single Python process due to global interpreter locks (GIL).
In terms of shortcomings, the above diagram also clearly shows the main limitations of this scheme. We have five clients, but only four worker. If all five clients send requests at the same time, the load balancer sends all requests except one client to the worker pool, while the remaining requests have to remain in a queue until worker becomes available. As a result, 4/5 of the requests will respond immediately, while the remaining 1/5 will take a while. One of the keys to server optimization is to select an appropriate number of worker to prevent or minimize request blocking for a given expected load.
This type of server runs in a single process and is controlled by a loop. This loop is a very efficient task manager and scheduler that creates tasks to execute requests sent by the client. Unlike the long-standing server worker, asynchronous tasks are created by loops to process a particular request, and when that request is completed, the task is destroyed. At any given time, an asynchronous server will have hundreds or thousands of active tasks that perform their work under the management of a loop.
You may wonder how parallelism between asynchronous tasks is implemented. This is the interesting part, because an asynchronous application does this through unique collaborative multitasking. What does that mean? When a task needs to wait for an external event (for example, a response from a database server), instead of waiting like a synchronous worker, it tells the loop what it needs to wait for and returns control to it. The loop can find another ready task when the task is blocked by the database. Eventually, the database will send a response, and the loop will assume that the first task is ready to run again and will restore it as soon as possible.
This ability to suspend and resume execution of asynchronous tasks can be difficult to understand abstractly. To help you apply what you already know, consider using the await or yield keyword in Python, but you will later find that this is not the only way to implement asynchronous tasks.
It is surprising that an asynchronous application runs entirely in a single process or thread. Of course, this type of concurrency needs to follow some rules, so you can't let a task occupy CPU for too long, otherwise, the remaining tasks will be blocked. In order to execute asynchronously, all tasks need to be periodically actively paused and control is returned to the loop. In order to benefit from the asynchronous approach, an application needs to have tasks that are often blocked by I CPU O and do not have much work on it. Web applications are usually well suited, especially when they need to handle a large number of client requests.
When using an asynchronous server, to maximize the utilization of multiple CPU, you usually need to create a hybrid scheme, add a load balancer, and run an asynchronous server on each CPU.
Two methods to realize Asynchronous in Python
I'm sure you know that to write an asynchronous application in Python, you can use asyncio package, which implements the pause and resume features that all asynchronous applications need on a collaborative basis. The yield keyword, as well as the updated async and await, are the basis on which asyncio builds asynchronous capabilities.
Https://docs.python.org/3/library/asyncio.html
There are other asynchronous schemes based on co-programming in the Python ecosystem, such as Trio and Curio. Then there is Twisted, which is the oldest of all collaborative frameworks and even predates asyncio.
If you are interested in writing asynchronous Web applications, there are many asynchronous frameworks based on protocols to choose from, including aiohttp, sanic, FastAPI, and Tornado.
What many people don't know is that co-programming is just one of two ways to write asynchronous code in Python. The second method is based on a library called greenlet, which you can install with pip. Greenlets and co-programs are similar, they also allow a Python function to suspend execution and resume later, but they do this in a completely different way, which means that the asynchronous ecosystem in Python is divided into two broad categories.
The most interesting difference between collaboration and greenlets for asynchronous development is that the former requires Python language-specific keywords and features to work, while the latter does not. I mean, co-program-based applications need to be written in a specific syntax, and greenlet-based applications look almost the same as normal Python code. This is cool because, in some cases, it allows synchronous code to be executed asynchronously, which is not possible with a protocol-based scheme such as asyncio.
So in terms of greenlet, what are the equivalent libraries of asyncio? I know three greenlet-based asynchronous packages: Gevent, Eventlet, and Meinheld, although the last one is more like a Web server than a general-purpose asynchronous library. They all have their own asynchronous loop implementations, and they all provide an interesting "monkey-patching" feature that replaces blocking functions in the Python standard library, such as those that execute networks and threads, and implements an equivalent non-blocking version based on greenlets. If you have some synchronous code that wants to run asynchronously, these packages will help you.
As far as I know, the only Web framework that explicitly supports greenlet is Flask. The framework automatically monitors, and when you want to run on a greenlet Web server, it adjusts itself accordingly without any configuration. When doing this, you need to be careful not to call blocking functions, or, if you want to call blocking functions, it is best to use monkey patches to "fix" those blocking functions.
However, Flask is not the only framework that benefits from greenlets. Other Web frameworks, such as Django and Bottle], although they do not have greenlets, can be run asynchronously by combining a greenlet Web server and using monkey-patching to fix blocking functions.
Is async faster than synchronization?
There is a widespread misconception about the performance of synchronous and asynchronous applications-asynchronous applications are much faster than synchronous applications.
In this regard, I need to clarify. Whether you write synchronously or asynchronously, Python code runs at almost the same speed. In addition to code, there are two factors that can affect the performance of a concurrent application: context switching and extensibility.
Context switching
Sharing the work required by CPU fairly among all running tasks, called context switching, can affect the performance of the application. For synchronous applications, this is done by the operating system and is basically a black box that does not require configuration or fine-tuning options. For asynchronous applications, context switching is done by loops.
The default loop implementation is provided by asyncio and written in Python, which is not very efficient. The uvloop package provides an alternative loop, some of which is written in C for better performance. The event loop used by Gevent and Meinheld is also written in C. Eventlet uses loops written by Python.
Highly optimized asynchronous loops are more efficient than operating systems in context switching, but in my experience, to see actual efficiency gains, you have to run a lot of concurrency. For most applications, I don't think the performance gap between synchronous and asynchronous context switching is significant.
Expansibility
I think the source of the myth that asynchrony is faster is that asynchronous applications tend to use CPU more efficiently, scale better, and extend in a more flexible way than synchronization.
If the synchronization server in the diagram above receives 100 requests at the same time, think about what will happen. The server can only handle up to four requests at a time, so most requests stay in a queue until they are assigned a worker.
By contrast, the asynchronous server creates 100 tasks immediately (or 25 tasks on each of the four asynchronous worker in mixed mode). With an asynchronous server, all requests are processed immediately without waiting (although, to be fair, there are other bottlenecks that slow down, such as restrictions on active database connections).
If these 100 tasks mainly use CPU, then synchronous and asynchronous scenarios will have similar performance, because each CPU runs at a fixed speed, Python executes code at the same speed, and the application does the same work. However, if these tasks require a lot of CPU O operations, then the synchronization server can only handle four concurrent requests and cannot achieve high utilization of ICPO. Asynchronous servers, on the other hand, are better able to keep CPU busy because it runs all 100 requests in parallel.
You may wonder why you can't run 100 synchronous worker, so that the two servers will have the same concurrency capability. Note that each worker requires its own Python interpreter and all resources associated with it, plus a separate copy of the application and its resources. The size of your server and application will determine how many worker instances you can run, but usually this number is not very large. On the other hand, asynchronous tasks are very lightweight and run in the context of a single worker process, so they have a significant advantage.
To sum up, only in the following scenarios, we can say that asynchrony may be faster than synchronization:
There is a high load (without high load, high concurrency of access has no advantage)
The task is bound by CPU O (concurrency exceeding the number of CPU does not help if the task is bound)
You look at the average number of requests processed per unit time. If you look at the processing time of a single request, you won't see much difference, and even asynchrony may be slower, because async has more concurrent tasks competing for CPU.
After reading this, the article "what's the difference between Python synchronization and asynchronism" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.