[python-win32] win32 file locking methods
Sheila King
sheila@thinkspot.net
Mon, 27 Aug 2001 16:49:48 -0700
OK, after a suggestion from David Bolen in the comp.lang.python
newsgroup, I tried this:
PythonWin 2.1.1 (#20, Jul 20 2001, 01:19:29) [MSC 32 bit (Intel)] on
win32.
Portions Copyright 1994-2001 Mark Hammond (MarkH@ActiveState.com) - see
'Help/About PythonWin' for further copyright information.
>>> import win32file
>>> from win32con import *
>>> fd = win32file.CreateFile(r'e:\temp\gpl.txt', GENERIC_READ, 0, None, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
>>> GD = win32file.CreateFile(r'e:\temp\gpl.txt', GENERIC_READ, 0, None, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
api_error: (32, 'CreateFile', 'The process cannot access the file
because\r\nit is being used by another process.')
>>> win32file.CloseHandle(fd)
>>> GD = win32file.CreateFile(r'e:\temp\gpl.txt', GENERIC_READ, 0, None, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
>>> win32file.CloseHandle(GD)
>>>
It seems to work quite well???
comments? I am excited about this, unless someone has some reason to
caution me or point out why this won't work. To recap: I want to be able
to have file locking (on an entire file; I don't require locks on only
parts of the file), on win32, including win98 as well as NT/2000. This
looks like it may be the way to go.
Here is the exchange from comp.lang.python:
David's suggestion:
> After reading this thread, it appears that you probably want more fine
> grained locking, but just in case you do end up falling back to some
> sort of sentinel file or more global lock approach...
>
> An alternative to the msvcrt lock stuff for a full file lock that may
> be more portable among Windows platforms would be to just open your
> lock file using the native Win32 CreateFile call (wrapped in
> win32file), and ask for exclusive access (no sharing mode). If you
> need to do I/O to the lock file you'll probably need to use the Win32
> functions (although you could wrap that in a file-like object), but if
> you're just using that file to restrict access to other files it can
> be a convenient method.
My response:
> :After reading this thread, it appears that you probably want more fine
> :grained locking, but just in case you do end up falling back to some
> :sort of sentinel file or more global lock approach...
>
> No, I wasn't trying to do fine grained locking. I was going to use a
> sentinel file.
>
> :An alternative to the msvcrt lock stuff for a full file lock that may
> :be more portable among Windows platforms would be to just open your
> :lock file using the native Win32 CreateFile call (wrapped in
> :win32file), and ask for exclusive access (no sharing mode).
>
> Thanks. I'm looking at that, and it looks like it might be a very good
> approach.
>
> :If you need to do I/O to the lock file you'll probably need to use the Win32
> :functions (although you could wrap that in a file-like object),
>
> Fortunately, I'm not planning any I/O on the locked file.
>
> :but if you're just using that file to restrict access to other files it can
> :be a convenient method.
>
> This is exactly what I'm trying to do. Thanks, again, for the
> suggestion.
--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/