Help: using msvcrt for file locking

Sheila King sheila at spamcop.net
Sun Aug 26 17:01:26 EDT 2001


On Sun, 26 Aug 2001 23:10:01 +0200, Richard van de Stadt
<stadt at cs.utwente.nl> wrote in comp.lang.python in article
<3B8965A9.8687AE9C at cs.utwente.nl>:

: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()

Well, maybe it would if I were content to run only on a posix system. I
was trying to do something cross-platform (Unix/Windows).

: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 :-)

Maybe you need to update your code. The docs on the posix module (here:
http://www.python.org/doc/current/lib/module-posixfile.html )
state:
"
Note: This module will become obsolete in a future release. The locking
operation that it provides is done better and more portably by the
fcntl.lockf() call. 
"

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/




More information about the Python-list mailing list