Is "shelve" thread-safe on Windows?

Fredrik Lundh fredrik at pythonware.com
Thu Jul 29 11:26:38 EDT 1999


I wrote:
>
>     http://www.python.org/doc/current/lib/msvcrt-files.html
> 
>     msvcrt.locking(file.fileno(), mode, bytes)
> 
> if anyone knows how to use the "mode" argument,
> please let us know...

okay, okay.  Snipped from the Microsoft
docs:

    The locking function locks or unlocks nbytes
    bytes of the file specified by handle. Locking
    bytes in a file prevents access to those bytes
    by other processes. All locking or unlocking
    begins at the current position of the file
    pointer and proceeds for the next nbytes
    bytes. It is possible to lock bytes past end
    of file.

    mode must be one of the following manifest
    constants:

    _LK_LOCK = 1 # lock the file region

    Locks the specified bytes. If the bytes cannot
    be locked, the program immediately tries again
    after 1 second. If, after 10 attempts, the bytes
    cannot be locked, the constant returns an error.

    _LK_NBLCK = 2 # non-blocking lock

    Locks the specified bytes. If the bytes cannot
    be locked, the constant returns an error.

    _LK_NBRLCK = 4 # non-blocking lock for writing)

    Same as _LK_NBLCK.

    _LK_RLCK = 3 # lock for writing

    Same as _LK_LOCK.

    _LK_UNLCK = 0 # unlock the file region

    Unlocks the specified bytes, which must have
    been previously locked.

    Multiple regions of a file that do not overlap
    can be locked. A region being unlocked must
    have been previously locked. locking does
    not merge adjacent regions; if two locked
    regions are adjacent, each region must be
    unlocked separately. Regions should be
    locked only briefly and should be unlocked
    before closing a file or exiting the program.

I'm sure Fred Drake would love if someone could take
this, rephrase it (so it doesn't look as it's cut right out
of Microsoft's copyrighted docs), and submit patches
to the library reference.  don't forget to emphasize
that you must use seek (or possibly os.seek(file.fileno())?)
to go to the beginning of the region before you issue
the locking command.

Extra bonus points if you tweak msvcrt so it exports
the various mode flags.

And even more bonus points if you add docstrings
to msvcrt.

Supermega many bonus points for anyone who
implements and documents a locking abstraction
which works on all major unixes *and* windows.

</F>





More information about the Python-list mailing list