IO Programming--Turn to Liao Xuefeng Blog

Source: Internet
Author: User

Io is the input/output in the computer, that is, input and output. Because the program and runtime data resides in memory and is executed by the CPU, an ultra-fast compute core, where the data is exchanged, usually a disk, a network, and so on, requires an IO interface.

For example, you open a browser, visit the Sina homepage, the browser this program needs to get Sina's web page via network IO. The browser first sends the data to the Sina server, tells it I want the homepage HTML, this action is outward sends the data, is called the output, then the Sina server sends over the webpage, this action is receives the data from the outside, is called input. So, typically, the program completes IO operations with input and output two data streams. Of course there is only one case, for example, to read from disk to memory, only input operation, in turn, writes the data to the disk file, is just an output operation.

In IO programming, stream (stream) is a very important concept that can imagine a stream as a water pipe, and the data is a pipe, but it can only flow in one direction. Input Stream is the data that flows from the outside (disk, network) into memory, and the Output stream is the data flowing from the memory to the outside. For web browsing, the browser and Sina server need to establish at least two water pipes, before they can both send data and collect data.

Because the CPU and memory speed is much higher than the speed of the peripheral, so in IO programming, there is a serious problem of speed mismatch. For example, for example, to write 100M data to disk, the CPU output 100M of data only need 0.01 seconds, but the disk to receive this 100M data may take 10 seconds, how to do? There are two ways to do this:

The first is the CPU waiting, that is, the program suspends execution of subsequent code, such as 100M of data in 10 seconds after writing to the disk, and then down execution, this mode is called synchronous IO;

Another way is that the CPU does not wait, just tell the disk, "you old slowly write, do not worry, I went on to do something else," then, the subsequent code can be executed immediately, this mode is called asynchronous IO.

The difference between synchronous and asynchronous is whether to wait for the results of IO execution. Like you go to McDonald's order, you say "to a hamburger", waiter told you, sorry, hamburger to do now, need to wait 5 minutes, so you stand in front of the cashier waiting for 5 minutes, get hamburger and then go shopping mall, this is synchronous IO.

You say "Come a hamburger", the waiter tells you, Hamburg needs to wait 5 minutes, you can go to the mall, wait for the good, we will inform you, so you can immediately do other things (shopping mall), this is asynchronous IO.

Obviously, using asynchronous IO to write program performance is much higher than synchronous IO, but the disadvantage of asynchronous IO is the complexity of the programming model. Think about it, you need to know when to tell you that "burger is ready", and the method of informing you is different. If the waiter came to find you, this is the callback mode, if the waiter texted you, you have to keep checking the phone, this is the polling mode. In summary, the complexity of asynchronous IO is much higher than that of synchronous IO.

The ability to operate IO is provided by the operating system, and each programming language encapsulates the low-level C interface provided by the operating system for ease of use, and Python is no exception. We'll discuss Python's IO programming interface in detail later.

Note that this chapter of IO programming is synchronous mode, asynchronous IO Due to the complexity is too high, subsequent to the development of server-side program is discussed.

IO Programming--Turn to Liao Xuefeng Blog

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.