What happens if the same file is read and added at the same time (python programming)?

I am writing a script using two separate streams, one of which performs a file reading operation, and the other one adds, both of which work quite often.

My question is, if one thread is reading a file and the other is in the middle of additional lines, such as “This is a test” in this file, what will happen?

I know if you add a line smaller than the buffer , no matter how often you read the file in other streams, there will never be such an incomplete line as “This i” appearing in your read file, I mean, what os will either do: add "This is a test" → read information from a file; or: read information from a file → add “This is a test” to the file; and this will never happen: add "This i" → read information from the file → append "s test".

But if "This is a test" is large enough (assuming it is a string larger than the buffer) ), os cannot add the task to one operation, so divide the add task by two: first add "This i" to the file, then add "sa test", so in this situation, if I happen to read the file in the middle of the whole upload operation, will I get the following result: append "This i" → read information from the file → add "sa test", which means that I can read a file containing an incomplete line?

+4
source share
1 answer

If you are worried about this, just ask your consumer to find a special character (the endline will work) so that he knows that there was no incomplete record. Thus, your producer (the one that writes data to the file) can output partial data, but the consumer (one reading from the file) will know that he received only a partial write.

Is there a reason why you are not using PIPE instead of a file? And is there a reason you use streams? You really do not gain anything, with the possible exception of coding simplicity, but IMO you can have separate processes, then you can get a gain from this model.

Added: Unfortunately, this I / O stuff is not only how Python handles things, but also how the OS handles everything. Everything you said about worrying about the buffer is true.

http://docs.python.org/library/functions.html#open

I would try to figure out what buffer size you have, and for that I don’t even know how to check. I use OSX anyway.

+1
source

Source: https://habr.com/ru/post/1334914/


All Articles