I am developing a software in python. It receive videos and detect if there are any porn moments, and if it detects some porn moments on this video, this software would record the video name and the specific time to a log file. If I have 2 movies ('Titanic' and 'SAMSARA') I will run this software 2 times given different movie path and after several minutes I can get the following log file generated by my softwares.
ID video_name porn_time(h:m:s)
1 Titanic 1:12:23
2 Titanic 1:29:10
3 SAMSARA 0:45:50
4 Titanic 1:56:00
5 SAMSARA 0:49:56
Every time I want to write new row, I would read the log file to know the ID of the last row, then I set a new_ID = ID + 1, and write the new_ID and new row into the log file.
I found the running software should check if the log file is open by other softwares (especially when a lot of movies are detected at the same time), otherwise the ID would be wrong or it will fail to write new row to the log file.
I hope to find a way to check if the log file is open by other softwares. If so, I can wait several seconds to let others' softwares finish their job and close it. The code would be:
while True:
if file_is_open():
time.sleep(3)
else:
write_data_to_log_file()
break
Tavy gave a solution but I found it does not work on my server with linux system.