23

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?

Andreas
  • 541
  • 2
  • 8
  • 16

4 Answers4

22

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.

Blue Peppers
  • 3,718
  • 3
  • 22
  • 24
10

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

Michael Kuhn
  • 8,302
  • 6
  • 26
  • 27
  • 2
    You certainly got a point. I initially thought of this way of doing it, but changed my mind and went for file i/o, and once I encountered the problem and started wrestling with it, I sort of came to the point where I want to solve it :P. Thanks for the answer still. – Andreas Jul 09 '10 at 09:40
3

Take a look at this Read-Write Lock class:

and at this articles about locking and threading:

miku
  • 181,842
  • 47
  • 306
  • 310
1

Another great way to do this is to use a pipe

This example instantiates a pipe, which returns a reader and a writer object. Then one process writes to the pipe using the writer and the other reads from it using the reader.

Python os.pipe(): https://docs.python.org/3/library/os.html#os.pipe

spacether
  • 2,136
  • 1
  • 21
  • 28