views:

86

answers:

3

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?

+2  A: 

Take a look at this Read-Write Lock class:

and at this articles about locking and threading:

The MYYN
+4  A: 

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

biocs
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
+5  A: 

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
Yes, this worked fine for me too, thanks.
Andreas