Turn: Python twisted tutorial, asynchronous programming

Source: Internet
Author: User

Python twisted tutorial 1: asynchronous programming

Published in: August 17,201 1, PM by twisted: cyrec read: [214]

From: http://www.notedit.com/

Http://krondo.com/blog? P = 1209

 

PrefaceRecently, someone asked in the twisted email list whether there is a document that allows you to quickly learn twisted. in general, this series is not such a document. if you don't have much time or patience, this series of articles is not suitable for you. however, if you have little knowledge about asynchronous programming, I believe that a brief introduction will not let you fully understand it, unless you are a genius. I have been studying and using twisted for several years. Through these years of study and work, I have come to the following conclusions: the difficulty in learning twisted is to understand asynchronous programming, rather than how to use the twisted function to write code. twisted code is concise and clear, and has good comments and documents, however, if you do not understand the idea of asynchronous programming and directly read the source code of twisted, you will feel twisted. the first article in this series will talk about asynchronous programming, and the subsequent chapters will be dropped to twisted. first, let's use a simple example to illustrate how asynchronous programming works. Model
To better understand the asynchronous model, let's first review some of the models we often encounter: Synchronous model and multithreading model. let's assume that a program needs to complete three tasks. Let's first look at how the synchronization model works. For example, Figure 1 shows the simplest way to execute tasks, it is also often used when we write programs. After completing a job, we will do another thing and only execute one task at a time. we can compare the multi-thread model of the synchronization model, for example, Image 2 in this model, each task is assigned to a separate thread, and multiple tasks can be performed at the same time. In this model, each task is considered independent. however, in reality, in many cases, each thread is not independent, and results need to be obtained from other threads during the running process. In this way, interaction and collaboration among various threads become complicated, in a large system, interaction between processes is more complex. finally, we will talk about the asynchronous model. for example, in the asynchronous model of Image 3, each task is alternating, but it is still in a process. the asynchronous model is simpler than the multi-threaded model, because the running status of each task can be controlled by us. although the synchronization model can also allow tasks to run alternately, it usually requires the collaboration of multiple threads to complete. the single-thread asynchronous mode ensures that the program runs in one thread, even in a multi-process system. another difference between the asynchronous model and multithreading model is that multithreading is controlled by the operating system in addition to program control. on the contrary, in an asynchronous model program, a task will continue to run until the task is completed or the program suspends the task to execute it. the important thing is that the asynchronous model can be well combined with the multi-threaded model, but in this series of tutorials we only involve the asynchronous model. Why (for Mao)
From the above explanation, we can see that the asynchronous model is simpler than the multi-threaded model, because the asynchronous model has only one process and the task stop and running status are controllable. but it is more complicated than the Synchronization Model. programmers must divide each task into multiple steps and combine them in an orderly manner. If a task uses the result of making a task, this task needs to accept the output of another task as its own input, and the received data is often a piece rather than a whole. since the asynchronous model and Synchronization Model are both a thread, it takes the same time for them to execute the same task, or even more time than the Synchronization Model, why do we need to adopt an asynchronous model? There are at least two reasons. First, if one task in multiple tasks is responsible for implementing the interface for interaction with one machine, other tasks can be executed while waiting for user input, process user input when user input. so if the asynchronous model is faster than the synchronous model, it is conditional. If your program is blocked or forced to wait, the asynchronous model will be your choice. the execution process of the Synchronization Model when there is congestion should be such an image. 4 The gray part in this figure indicates that a task is waiting (blocking ). why is a task blocked? A common cause is to wait for I/O execution to transmit data. generally, the CPU processes data faster than disks and network blocks. Therefore, it takes a lot of time to wait when a synchronization program processes a lot of I/O, such a synchronization program is also called a "blocking program". Note that image 4 is a blocking program, a bit like image 3, an asynchronous program. this is not a coincidence. The design principle of the asynchronous model is that when one of the tasks is blocked, you can first execute other executable tasks. therefore, an asynchronous program is called a non-blocking program only when no task can be executed. if a program has many blocked tasks, the asynchronous model can be more efficient than the synchronous model. compared with the Synchronization Model, the asynchronous model performs better in the following situations: 1. There are many tasks and there is always a task that can be executed continuously; 2, these tasks require many I/O operations. 3. Most of these tasks are independent. Most of these cases describe a very busy web server, each task represents a request receiving and sending result, and most of these client requests are independent. Therefore, the implementation of a web server provides a good asynchronous model, this is the network programming library called twisted. Onward and upwardThe first part of this series is complete. In the second part, we will write some network programs, blocking and non-blocking, without using twisted. let's first understand how an asynchronous program runs.
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.