File Locking (or any similar file mutex) in Windows OS???

Cliff Wells logiplexsoftware at earthlink.net
Wed Jul 3 21:07:45 EDT 2002


On Wed, 2002-07-03 at 16:45, Ron Horn wrote:

> I would like some simple and robust way to know if one of the clients is
> currently using the file, but my question could apply to any shared resource
> the various client instances could want to share.  It seems that the various
> file locking features of os.open(), and anydbm.open(), etc. are not working
> right on Windows?
> 
> Any suggestions on how to make this work in python running on windows
> clients/server would be appreciated.

You could write a "lock-server": a process that runs on the server (or
any single PC, for that matter) that listens on a TCP port.  When a
client wants to lock a file, it can pass the filename of the file to the
lock server and if the lock server returns a certain value (0, 1, 'r',
'w', etc), the client knows whether it is safe to access the file (or
resource).  When the client is finished it simply disconnects from the
server and the server frees up that resource (freeing on disconnect is
good as it allows the resource to be freed in case of a client crash as
well).

This approach doesn't enforce locking (it requires that the clients
honor the status returned by the server), but it should be fairly simple
to implement.  I'd consider using Pyro or something similar to simplify
it even more.

Regards,
Cliff






More information about the Python-list mailing list