Two processes reading/writing to the same file Pyt

2020-05-20 06:45发布

问题:

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?

回答1:

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.



回答2:

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



回答3:

Take a look at this Read-Write Lock class:

  • http://code.activestate.com/recipes/502283-read-write-lock-class-rlock-like/

and at this articles about locking and threading:

  • http://effbot.org/zone/thread-synchronization.htm
  • http://linuxgazette.net/107/pai.html


回答4:

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