Help: using msvcrt for file locking

Richard van de Stadt stadt at cs.utwente.nl
Sun Aug 26 17:10:01 EDT 2001


Sheila King wrote:
[...]
> 
> For CGI, I need file locking. The alternatives are (as I understand
> them):
> 
> 1. Have a server running, which controls access to the files, thus
> ensuring that only one process can write to the files at a time.
[...]

I missed the start of this thread, but doesn't then this work for you:

def oneAtTheTime():
	f = posixfile.open (someFilename, 'a')
	f.lock ('w|')
	f.write ('something') # or do something else that no other process may do at this time
	f.lock ('u')
	f.close()

I've been using this for over 5 years. It controls access to a particular
file for a system that collects submissions for conferences, where most
submissions have to be dealt with very close to the deadline. (Designed
in order to deals with multiple webform submissions, it also saved me a
couple of times when a fileserver broke down. The submissions where
simply blocked on the webserver until the fileserver came up again a
few days later :-)

Richard.



More information about the Python-list mailing list