Python--->io mode (for 0 Basics)

Source: Internet
Author: User
Tags epoll


Thanks to the guidance of our predecessors, here is my own understanding, some pictures and passages from http://www.cnblogs.com/alex3714/articles/5876749.html

Http://www.cnblogs.com/Anker/p/3254269.html

If you encounter problems during your learning process, please contact: 2775724349 (because of their limited time, so may not often reply, but will collect each week to meet the problem, in Saturday to blog in the form of post)

Blocking I/O (blocking IO)

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/720333/201609/720333-20160916171008617-1558216223. PNG "alt=" 720333-20160916171008617-1558216223.png (552x331) "/>

650) this.width=650; "Src=" http://images.cnitblog.com/blog/305504/201308/12230408- C8b30331f20a41dcb224d20719ffa1da.png "alt=" 12230408-c8b30331f20a41dcb224d20719ffa1da.png (722x325) "/>

When the process starts reading to read the data, use the RECV function to fetch data from memory, but, in memory, the data is not ready, how to do it, this time will continue to wait, wait until the data is ready, will end the blocking state, start copying data, and then send to the user.

A rough analogy: I go shopping with women tickets to cultivate feelings. Let's go to the restaurant and have a meal.

(1) My girlfriend and I after the meal, do not know when to do a good job, had to sit in the restaurant and so on, until the good, and then eat out before leaving.

My girlfriend would like to go shopping with me, but I do not know when the meal can be done, and I have to and I together in the restaurant and so on, and can not go shopping, until after eating dinner before going shopping, in the middle of waiting for the cooking time wasted. This is the typical blockage.

In a word;

The blocking Io is characterized by block in both phases of IO execution.

Non-blocking I/O (nonblocking IO)

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/720333/201609/720333-20160916171226852-1916489268. PNG "alt=" 720333-20160916171226852-1916489268.png (603x333) "/>

650) this.width=650; "Src=" http://images.cnitblog.com/blog/305504/201308/ 12231306-35dca310d92e4184bd4c0b3f42bee2c1.png "alt=" 12231306-35dca310d92e4184bd4c0b3f42bee2c1.png (642x342) "/ >

When a user wants to use data, call the RECV function, send a message to memory, but Memory says: Big Brother, I'm not ready yet. Then returns an error (message) to the user, but the user's system is a short temper, thought: The eldest brother asked me to AH. You have to hurry, too. Finish, and continue to send a message to the kernel, until the data is ready to end the access

Example:

(2) My girlfriend is not willing to wait in vain, and want to go shopping malls, but also worried about the food good. So we stroll for a while, come back to ask the waiter meal good no, to go back and forth several times, the meal has not eaten is almost exhausted. This is non-blocking. Need constant questioning, are you ready?

I/O multiplexing (IO multiplexing)

Io Multiplexing is what we call Select,poll,epoll, and in some places this IO mode is the event driven IO. The benefit of Select/epoll is that a single process can simultaneously handle multiple network connections of IO. The basic principle of the select,poll,epoll is that the function will constantly poll all sockets that are responsible, and when a socket has data arrives, notifies the user of the process.

This is beginning to become complicated, and then I try to say plain English. First:

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/720333/201609/720333-20160916171333523-650292614. PNG "alt=" 720333-20160916171333523-650292614.png (609x326) "/>

650) this.width=650; "Src=" http://images.cnitblog.com/blog/305504/201308/ 12232204-08fdf03d1d8a40a68d2286c6c43fb036.png "alt=" 12232204-08fdf03d1d8a40a68d2286c6c43fb036.png (663x352) "/ >

To enhance our understanding, I will first compare the third to the first two.

For the first and second blocking I/O (blocking IO): If in a single thread to take this scenario, a collection of data, blocking, a packet of data blocking, then if I want to collect more than a few data how to do it, it is not playing, so at this time can not be in multiple sockets to receive data, Even if the data continues to come back, you have to wait, because the front is jammed.

But for this kind of, if single-threaded to 100 socket to send data, and then in the 100 sockets for A For loop, waiting for their data, a loop, received 5 data, then began to copy the data, sent to the user, someone will think, that the other, will not care, because, I have received the data.

The Select,poll,epoll function is to continuously cycle and filter.

Repeat the Select connection on the 100 socket handle, and then the kernel starts to detect the 100 handles, even if one person receives the data, he returns the data

Next, introduce a more bull B IO

asynchronous I/O (asynchronous IO)

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/720333/201609/720333-20160916171458461-2052304822. PNG "alt=" 720333-20160916171458461-2052304822.png (572x324) "/>

650) this.width=650; "Src=" http://images.cnitblog.com/blog/305504/201308/ 12233159-16ec6876a48d424d8e6b524d1fb91689.png "alt=" 12233159-16ec6876a48d424d8e6b524d1fb91689.png (656x334) "/ >

The asynchronous IO first calls the function to send a notification to the kernel, but the kernel says I have no data, returns a message, and then the user begins to do something else, and when the kernel copies the data, it calls the function directly to the user, and maybe later a bit of the conversation is foggy, with a drawing analogy:

I and the female vote is not to go out to eat, we do not go out, directly in the home point of sale, external selling, just start to eat, eat, you can go out to play directly. , in this period, I and the female votes love why do, the outside sell all not, to the user make no influence.

Equivalent to the kernel to help us wait, but we do not need to wait. The difference between this place and the three is that the first three, after the data is ready, users still need to read, read the process, may be card, but this will never card.

non-blocking io is the blocking IO when performing recvfrom this system call, if the kernel data is not ready, this time will not block process. However, when the data in the kernel is ready, recvfrom copies the data from the kernel to the user's memory, at which point the process is blocked, during which time the process is block.

The asynchronous IO is not the same, and when the process initiates an IO operation, the direct return is ignored until the kernel sends a signal telling the process that IO is complete. Throughout this process, the process has not been blocked at all.

Four different picture comparisons

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/720333/201609/720333-20160916171648430-240094129. PNG "alt=" 720333-20160916171648430-240094129.png (614x327) "/>

Okay, that's it. The last kind of the most bull B,


Python--->io mode (for 0 Basics)

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.