I need a Python script where a background thread writes data to a file, while the main thread might read from it.
I know writting to a file from multiple thread can be problematic, but is it OK if only one thread opens the file in write mode, while another opens the file in read mode only?
I made a small test program and it worked so far, but could this create problems in a more complex program?
from threading import Thread
from time import sleep
class Writter(Thread):
def __init__(self) -> None:
super().__init__()
def run(self):
print("Sampler started")
with open("testfile.txt", mode='w') as f:
i = 0
while True:
new_line = f"{i}\n"
f.write(new_line)
f.flush()
i += 1
sleep(1)
if __name__ == '__main__':
writter = Writter()
writter.start()
sleep(2)
with open("testfile.txt", mode='r') as f:
while True:
new_line = f.readline()
if new_line != '':
print(new_line)
I have used queues before in other projects, but it isn't suitable here. I need to write the data to a file, because the code that is reading the data isn't always run at the same time. I could do both, writting to a file and additionally write to a queue for when reader and writter run at the same time. But then I would have to add checks to the reader to see if it needs to read the file or if it can use the queue.
If the code above is ok, it would be simpler. Is there any way this could potentially break?
If you're asking from the concurrency point of view, it's ok. Only one thread writes to the file, multiple threads can read from the file without data get corrupted.
But another problem here is that if the writer thread writes slowly, the reader thread is going to loop without any sleep -> wastes cpu for nothing.
You can add sleep there too or use the projects like https://pypi.org/project/watchdog/, which basically notifies you if something is changed in the file system. That could be your case.