How to implement asynchronous IO with Python

Source: Internet
Author: User

In the IO programming section, we already know that the CPU is much faster than the disk, network and other IO. In one thread, the CPU executes the code very quickly, however, once the IO operation is encountered, such as reading and writing files, sending network data, it is necessary to wait for the IO operation to complete before proceeding to the next step. This condition is called synchronous IO.

During the IO operation, the current thread is suspended, and other code that requires CPU execution cannot be executed by the current thread.

Because one IO operation blocks the current thread, causing other code to fail, we must use multithreading or multiple processes to execute code concurrently, serving multiple users. Each user is assigned a thread that is not affected by another user's thread if it encounters an IO that causes the thread to be suspended.

Multithreaded and multi-process models solve concurrency problems, but the system cannot increase threads without an upper limit. Because the system switching threads is also very expensive, so once the number of threads is too high, the CPU time is spent on the thread switch, the real time to run the code is less, resulting in severe performance degradation.

Because of the problem we are dealing with is CPU high-speed execution capability and the turtle speed of the IO device is badly mismatched, multithreading and multi-process are just one way to solve this problem.

Another way to solve the IO problem is asynchronous IO. 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.

It can be imagined that if the code written in the normal order is actually unable to complete the asynchronous IO:

<== 线程停在此处等待IO操作结果# IO操作完成后线程才能继续执行:do_some_code(r)

Therefore, the code of the synchronous IO Model cannot implement the 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.

Because GUI threads handle messages such as keyboards and mice very quickly, users do not feel the delay. At some point, the GUI thread encountered a problem in the process of a message processing, resulting in a message processing time is too long, at this time, the user will feel the entire GUI program stopped responding, hit the keyboard, click the mouse did not respond. This situation shows that in the message model, processing a message must be very fast, otherwise the main thread will not be able to handle other messages in the message queue in a timely manner, causing the program to appear to stop responding.

How does the message model solve the problem that synchronous IO must wait for IO operations? 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.

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.

How to implement asynchronous IO with Python

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.