Python path--python base 12--asynchronous Io, redis\memcached cache, RABBITMQ queue

Source: Internet
Author: User
Tags memcached rabbitmq

First,Event-driven and asynchronous IO

Review: Synchronous, asynchronous, blocking, non-blocking

Synchronous:

The so-called synchronization is that when a function call is made, the call does not return until the result is obtained. By this definition, the vast majority of functions are synchronous invocations. But generally speaking, we are talking about synchronous and asynchronous tasks, especially those that require other components to collaborate or need to be done in a certain amount of time.

Example:

1. Multiprocessing. The Apply #发起同步调用后 under the pool, just wait for the task to end, do not consider whether the task is in the calculation or IO blocking, in short, a brain to the end of the task

2. Concurrent.futures.ProcessPoolExecutor (). Submit (func,). Result ()

3. Concurrent.futures.ThreadPoolExecutor (). Submit (func,). Result ()

Asynchronous:

  Asynchronous concepts and synchronization are relative. When an asynchronous function call is issued, the caller cannot get the result immediately. Notifies the caller by status, notification, or callback when the asynchronous function is complete. If the asynchronous function is notified by state, then the caller will need to check every time, the efficiency is very low (some beginners of multithreaded programming, always like to use a loop to check the value of a variable, which is actually a very serious error). If you are using notifications, the efficiency is high, because asynchronous functionality requires little extra action. As for the callback function, there is not much difference from the notification.

Example:

1. Multiprocessing. Pool (). Apply_async ( ) #发起异步调用后 does not wait for the end of the task to return, instead, it immediately gets a temporary result (not the final result, possibly an encapsulated object).

2. Concurrent.futures.ProcessPoolExecutor (3). Submit (Func,)

3. Concurrent.futures.ThreadPoolExecutor (3). Submit (Func,)

Blocking:

  A blocking call means that the current thread is suspended (such as an IO operation) before the call results are returned. The function will activate the blocked thread only after it has obtained the result. Someone might equate blocking calls with synchronous calls, and in fact he is different. For synchronous calls, many times the current thread is still active, but logically the current function does not return.

Example:

1. Synchronous Call: Apply a cumulative 100 million-time task, the call will wait until the task returns the result, but not blocked (that is, the execution of the CPU has been robbed, it is in the ready state);

2. Blocking call: When the socket is working in blocking mode, if the recv function is called without data, the current thread will be suspended until there is data.

Non-blocking:

The concept of non-blocking and blocking corresponds to a return immediately before the result is not immediately available, and the function does not block the current thread.

For a network IO (here we read for example), it involves two system objects, one that calls the IO process (or thread), and the other is the system kernel (kernel). When a read operation occurs, the operation goes through two stages:

1) Wait for data preparation (waiting for the

2) Copy the data from the kernel into the process (Copying the data from the kernel-the process)

It is important to remember these two points, because the difference between these IO models is that there are different situations in both phases.

1, input operation: Read, READV, recv, Recvfrom, recvmsg a total of 5 functions, if blocking the state, will be manager wait data and copy data two stages, if set to non-blocking will not wait for data when the exception is thrown

2, output operation: Write, Writev, send, SendTo, sendmsg a total of 5 functions, in the send buffer full will block in situ, if set to non-blocking, will throw an exception

3, receive External links: accept, similar to the input operation

4. Initiating out-of-Office Links: Connect, similar to output operation

Typically, when we write a program that server processes a model, there are several models:

1. Each request is received and a new process is created to process the request;

2. Each request is received and a new thread is created to process the request;

3, each received a request, put in an event list, let the main process through non-blocking I/O way to process the request

There are several ways that are different ,

The 1th method, because the cost of creating a new process is relatively large, resulting in poor server performance, but the implementation is relatively simple.

The 2nd way, due to the synchronization of threads, may face deadlock and other problems.

The 3rd way, when writing application code, logic is more complex than the previous two types.

taking into account various factors, it is generally accepted that (3) is the way most Web servers are used

Look at the diagram and talk about the event-driven model

in UI programming, it is often necessary to click on the mouse to the corresponding, first how to get a mouse click?
Mode One: Create a thread that has been looping through the detection of mouse clicks, and there are several drawbacks to this approach :
1. CPU waste, may be mouse click frequency is very small, but the scan thread will still be cyclic detection, which will cause a lot of CPU waste, if the scan mouse click on the interface is blocked?
2. If it is blocked, and will appear the following problems, if we not only to scan the mouse click, but also to scan the keyboard is pressed, because scanning the mouse is blocked, then may never go to scan the keyboard;
3. If a cycle needs to scan a lot of devices, which will lead to response time problems;
So, the way is very bad.

mode Two: is the event-driven model
most of the current UI programming is an event-driven model, as many UI platforms provide the OnClick () event, which represents the mouse down event. The event-driven model is broadly thought of as follows:
1. There is an event (message) queue;
2. When the mouse is pressed, add a click event (message) to this queue;
3. There is a loop that continuously extracts events from the queue, depending on the event, calling different functions, such as onclick (), OnKeyDown (), etc.;
4. Events (messages) generally hold their own handler pointers, so that each message has its own processing function;

second, blocking io (blocking IO)

Python path--python base 12--asynchronous Io, redis\memcached cache, RABBITMQ queue

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.