2

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.

alwbtc
  • 28,057
  • 62
  • 134
  • 188
  • Each script is run in its own process so what you're looking for is Inter-Process Communication (IPC). IPC is dependent on your OS so you might want to add a tag for your OS type and version to the question tag list. –  Jan 13 '13 at 20:59
  • Maybe take a look at this post: http://stackoverflow.com/questions/186202/what-is-the-best-way-to-open-a-file-for-exclusive-access-in-python – Demian Brecht Jan 13 '13 at 21:06
  • What you describe is essentially a [DBMS](http://en.wikipedia.org/wiki/Database_management_system). Have you considered something like sqlite? – georg Jan 13 '13 at 21:34

1 Answers1

1

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.

Sibi
  • 47,472
  • 16
  • 95
  • 163