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: 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 disk, and then execute, this mode is called synchronous IO, the other way is the CPU does not wait, just tell the disk, "you old slowly write, not anxious, I went on to do something else" As a result, subsequent code can be executed immediately, and the difference between asynchronous IO synchronization and asynchronous is whether to wait for the results of IO execution. The advantage of Asynchrony is the high program performance, the disadvantage is that the programming model is complex. To see, you need to know when to inform 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 short, the complexity of asynchronous IO is much higher than synchronous IO as you go to McDonald's to order food, 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 go shopping, 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. 9.1 File read/write #!/usr/bin/pythonpoem = ' \1.dog2.cat3.rabbit ' f=file ('/tmp/poem ', ' W ') f.write (poem) f.close () #输出文件内容, method one #f =open ('/tmp/poem ', ' r ') #for line in F.readlines (): # Print (Line.strip ()) #f. Close ()
#输出文件内容, method two f=file ('/tmp/poem ') while true:line = F.readline () If Len (line) ==0:break print (Line.strip ()) F.close () Write the file and read the file is the same, the only difference is to call the open () function, the incoming identifier ' W ' or ' WB ' means to write a text file or write a binary file:>>> f = open ('/users/michael/test.txt ', ' W ')
>>> f.write (' Hello, world! ')
>>> f.close () You can write the file repeatedly by calling write (), but be sure to call F.close () to close the file. When we write a file, the operating system often does not immediately write the data to disk, but instead put it in memory cache, and then write slowly when idle. Only when the close () method is called does the operating system guarantee that all data that is not written is written to disk. The consequence of forgetting to call Close () is that the data may have been written only partially to the disk, and the remainder has been lost. Therefore, it is safe to use the WITH statement: with open ('/users/michael/test.txt ', ' W ') as F:
F.write (' Hello, world! ')
Two benefits of using close () 1. Turn off the number of open files 2. Make sure that the in-memory data is written to the hard disk F.flush () You can write the contents of the memory to the hard disk without closing the file. In Python, file reads and writes are done through file objects opened by the open () function. Using the WITH statement to manipulate file IO is a good habit.