Python concurrent Pinterest No. 0 (asynchronous I/O Note)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Refer to the Liao Xuefeng asynchronous I/O Chapter

First, why use asynchronous IO

    • CPU speed is much faster than disk, network and other IO (we have to solve the problem is the CPU high-speed execution capability and the IO device of the turtle speed serious mismatch)
    • Synchronous IO Model : One IO operation blocks the current thread, causing other code to fail, so we must execute the code concurrently using multithreading or multiple processes
    • However, the overhead of the system switching thread is also very large, so once the number of threads is too high, the CPU time is spent on the thread switch, the actual code time is less, resulting in a severe performance degradation
    • Asynchronous IO Model : When the code needs to perform a time-consuming IO operation, it emits only the IO instruction, does not wait for the IO result, and then executes the other code. After a period of time, when IO returns the result, the CPU is then notified of processing.

Second, asynchronous IO model

The asynchronous IO model requires a message loop in which the main thread repeats the process of "reading messages-processing messages" repeatedly:

loop = get_event_loop()while True:    event = loop.get_event()    process_event(event)

The message model was actually applied in the desktop application. The main thread of a GUI program is responsible for constantly reading messages and processing messages. All the keyboard and mouse messages are sent to the GUI program's message queue, which is then processed by the GUI program's main thread.

When an IO operation is encountered, the code is only responsible for issuing an IO request, not waiting for the IO results, and then directly ending this round of message processing, into the next round of message processing. When the IO operation is complete, an "IO complete" message is received, and the result of the IO operation can be obtained directly when processing the message.

Third, True • Why use asynchronous IO

During the time the IO request is received and the IO is completed, the main thread can only be suspended under the synchronous IO model, but under the asynchronous IO Model, the main thread is not resting, but it continues to process other messages in the message loop. In this way, under the asynchronous IO model, one thread can process multiple IO requests at the same time, and there is no action to switch threads. For most io-intensive applications, using asynchronous IO will greatly improve the multitasking capabilities of the system.

Four, the co-process Coroutine

Donald Knuth: Subroutine is a special case of co-process

The process appears to be a subroutine, but during execution, it can be interrupted inside the subroutine, then execute another subroutine, and return to it at the appropriate time to execute

A) co-process vs thread

    • The greatest advantage is the high execution efficiency of the process. Because the subroutine switch is not a thread switch, but is controlled by the program itself, therefore, there is no thread switching overhead, and multithreading ratio, the more the number of threads, the performance advantage of the association is more obvious.
    • The second big advantage is that there is no need for a multi-threaded lock mechanism, because there is only one thread, there is no simultaneous write variable conflict, in the process of controlling shared resources without locking, only need to judge the state is good, so the execution efficiency is much higher than multithreading.
    • Because the co-process is a thread execution, how do you take advantage of multicore CPUs? The simplest method is multi-process + co-progression, which takes full advantage of multicore, and maximizes the high-efficiency of the co-processes, which can achieve very good performance.

b) Python process implementation

Don t care, why not just Golang

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.