Wed, 15 Jan 2003
Locking files (or not) the win98 way...

My issue tracking software is multi- platform - I wrote it in pure Python, so theoretically it runs anywhere that Python is installed. Unfortunately, it's multi-user software, so must lock its database while one user is making changes - otherwise another user could make changes at the same time and BOOM there goes your issue database.

So far my users have been mostly Unix, Windows (NT variant) or MacOS X users, and they've been served well. Unfortunately, I've just had my first Windows 98 user try to use it, and the locking doesn't work there. I've borrowed the portalocker code from the Python Cookbook.

Here's latest in the "less useful" thread from the conversation on python-list from Steve Holden:

Cliff Wells wrote:
> Richard Jones wrote:
> > If anyone knows how to lock a file on win98, I'd really appreciate a hint.
>
> Win98 only supports locking entire computers, not files 
>
> Assuming 98 doesn't support locking files (which I believe to be true,
> but am willing to be corrected on), what I would probably do in this
> case is create a "lock server" using a network socket and have
> applications acquire a lock from it.  I expect you could even make the
> API mirror the portalock API (which I've never used and so can't comment
> on) and use it as a drop-in replacement.
>
> Even if 98 supported file locking, I expect this approach would be
> superior.
>

Clearly something quite advanced is required here. I spoke to the Microsoft
help desk.

After a three-hour session in which I educated them about the benefits of
Python and the features and facilities offered by the language, they came up
with the following suggestion: "Make use of the Microsoft Office locking
algorithm by creating a COM object encapsulation an instance of Word and
have it open a  document. If the document is already open then Word wil
report an error that your program can use to determine the lock is currently
set."

Brilliant: shows that all that money I spend on Microsoft support isn't
wasted. I tried it, but unfortunately the COM implementation in Word doesn't
report the error correctly. Microsoft have recorded this as a bug, and will
get back to me if ever anybody fixes it.

tongue-in-cheek-ly y'rs  - steve

The other side of the thread actually looks looks for a solution closer to the current one. It would involve detecting we're on Windows 98 and using the msvcrt module. Using that as a hint, I found another thread on the python-win32 mailing list. That thread winds up with Sheila King finding her solution:

filename = r'e:\apache\cgi-bin\pylinks\datalog.txt'

import win32con
import win32file
import pywintypes
from time import strftime, localtime, time

f = open(filename, 'a+')
fd = win32file._get_osfhandle(f.fileno())
win32file.LockFile(fd, 0, 0, 0xffff0000,0)
timestamp = strftime("%m/%d/%Y %H:%M:%S\n", localtime(time()))
f.write(timestamp)
win32file.UnlockFile(fd, 0, 0, 0xffff0000,0)
f.close()

So now I need to find a win98 Roundup user who is willing to try the code out :)

path: /python | permanent link |