0

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.

Jingnan Jia
  • 1,108
  • 2
  • 12
  • 28
  • I have an idea, but I'm not sure it's gonna work. Try to open and then immediately close the file, and if it fails, then the file is probably open elsewhere. – Roy Cohen Mar 06 '21 at 13:40
  • @RoyCohen No, it does not work on linux system. – Jingnan Jia Mar 06 '21 at 13:41
  • There's a lot of questions about how to ensure a single writer is accessing the file; many of them not Python-specific. The proper way to do this is with an operating system primitive, not with Python code (otherwise the result could be obsoleted by another program opening the file after you check but before you open). For a scalable solution, probably switch to SQLite which handles this elegantly, efficiently, and transparently. – tripleee Mar 06 '21 at 13:50

0 Answers0