pythonfilewaitpausing-execution

Is it possible to pause a python script till file is not in use?


I have a python script which updates some values in a file. This script may be used by more than 1 users, which means that file will be updated by multiple users. But 2 users shouldn't update the file at the same time. If a user is updating the file, the other user should wait for 1st user's script to be completed. Is it possible to check if a file is in use in python? If in use, script should wait, and as soon as it is NOT in use, it should continue and update the file.


Solution

  • You need some sort of file locking.

    I think this gives solution to your problem.

    Traditionally this is called a resource sharing problem solved using semaphores, Locking etc.