Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two processes reading/writing to the same file Python

Tags:

I have one process who's reading from a file (using file.read()) and one process who's writing to the same file (file.write()). The problem is it doesn't work - I get no errors but they can't operate at the same time. I've tried making the read and write operations none-blocking and then flushing the stream, as follows:

fcntl.fcntl(file, fcntl.F_SETFL, os.O_NONBLOCK) file.write(msg) file.flush() 

Am I completely misunderstanding it? How should one accomplish writing and reading to one file from different processes?

like image 404
Andreas Avatar asked Jul 09 '10 09:07

Andreas


People also ask

Can two processes read and write to the same file?

If you try to read at the same time someone else is writing, that's perfectly OK. The only issue is if you are trying to read a block that the writer is writing at the same time. In that cause, the data you get is unpredictable but you should be able to read.

Can two processes open same file?

During the actual reading and writing, yes. But multiple processes can open the same file at the same time, then write back. It's up to the actual process to ensure they don't do anything nasty. If your writing the processes, look into flock (file lock).

Can Python read and write to same file?

Python makes it easy to read & write to files with the help of built-in functions.

Can two processes simultaneously write to different positions in a single file?

Yes, the two processes will have their own file table entries.


2 Answers

test1.py

import os f = open('txt.txt', 'a', os.O_NONBLOCK) while 1:         f.write('asd')         f.flush() 

test2.py

import os f = open('txt.txt', 'r', os.O_NONBLOCK) while 1:     print f.read(3) 

This works fine for me.

like image 98
Blue Peppers Avatar answered Nov 15 '22 08:11

Blue Peppers


Is there a reason to use a common file? Inter-process communication is probably much easier using sockets.

like image 28
Michael Kuhn Avatar answered Nov 15 '22 10:11

Michael Kuhn