0

I have 2 python(3.9) programs. One program writes to a text file, and another reads from that same text file to occasionally to populate data onto a website. Sometimes the unfortunate occurrence happens where the reading and writing ends up happening simultaneously, so the reading program ends up with blank data. The result looks like an empty string(such as ' '), and blank data throws a wrench in the flow of my setup.

For reference, here is how I have my programs set up. Here is the first program 1, which writes to the file:

while 1==1: #to make an endless loop
    file = open("demo.txt", "w")
    data = file.write('data goes here')
    file.close()

    time.sleep(360)

and the reader program:

while 1==1: #for the endless loop
    file = open("demo.txt", "r")
    data = file.read()
    file.close()
    time.sleep(0.1)

The above works great until they end up reading/writing at the same time.

Is there an elegant way to fix this? I've come up an idea that seem to work - having the reader program use a while loop to check for blank data:

file = open("demo.txt", "r")
data = file.read()
while data == '':
    file = open("demo.txt", "r")
    data = file.read()
file.close()

This seems to work, but is there a better way? Maybe an inherent way for python to sort out reading from a file that is already open in another program? Any ideas would be appreciated.

Thanks!

EDIT: after the comments below I've done a bit of reading on read/write locks - seems like multithreading is the common solution to this problem. I'm trying to avoid threading, looking for something simple and succinct. The os.rename idea below seems like the right idea.

rick b
  • 11
  • 1
  • This is the classic readers-and-writers problem in operating systems. I've given you one link, but you'll likely need to research the topic for a solution that matches your particular implementation. Since you haven't supplied an actual code example, we can't give you any specific suggestions. Please repeat [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). – Prune Apr 16 '21 at 16:09
  • In such a case, it is better not to write to that file directly. Instead, write to a temporary file, and when you’re done and have closed the file, move that temporary file to the real one, using the `os.rename()` function. This is a so-called *atomic* function, so the reading process cannot happen to get in between the updating of the file. That is, the reading process will never see an empty file. – inof Apr 16 '21 at 16:10
  • @Prune thanks! I've updated my question with my source code. I'll also look into the link you provided. – rick b Apr 16 '21 at 16:18
  • @inof that's a great idea. I'll definitely look into that! Thanks! – rick b Apr 16 '21 at 16:19
  • If you want a useful response, you have to make a clear problem statement. You very clearly state above that you have two separate programs, but then you claim that you are trying to avoid threading -- a direct contradiction. – Prune Apr 17 '21 at 23:34
  • Also, you still need to work through a tutorial on file handling before you attack something with interleaved reads & writes. Your posted code shows you opening a file multiple times with only one close, which shows that you are not yet clear on the concepts of file handling, nor on critical blocks of code. – Prune Apr 17 '21 at 23:36

0 Answers0