I have a Threaded class in which I am trying to append data but it is not doing anything, no error no success. The following is the basic structure of code
from threading import Thread
class ABC:
def func1(self, user):
# Do processing
self.func2(lst) # lst Generated in processing
def func2(self, lst):
thrd_list = []
for ele in lst:
x = Thread(target=self.func3, args(ele, ))
x.start()
thrd_list.append(x)
for thrd in thrd_list:
thrd.join()
def func3(self, ele):
# Do some stuff and if successful write to file
OUTPUT.write(f"{ele}\n")
with open("users.txt", "r") as f:
users = f.readlines()
OUTPUT = open("result.txt", "a")
thrd_list = []
for user in users:
new_obj = ABC()
x = Thread(target=new_obj.func1, args(user, ))
x.start()
thrd_list.append(x)
for thrd in thrd_list:
thrd.join()
OUTPUT.close()
The Data is being written on encounter of OUTPUT.close(). I want it to append as it goes so there is no data loss due to crash or bugs
The data being written to the file is being buffered, and only when the buffer is full or the file is closed is the buffered data written to the file.
Try changing the buffering to be line-buffered:
OUTPUT = open("result.txt", "a", buffering=1)
See https://docs.python.org/3/library/functions.html#open for more details.