[Python-Dev] Does Python need a file locking module (slightly higher level)?

Barry Warsaw barry at python.org
Mon Oct 29 15:30:32 CET 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Oct 26, 2007, at 4:10 PM, skip at pobox.com wrote:

>
>     Barry> I don't think any one solution will work for everybody.   
> I'm not
>     Barry> even sure we can define a common API a la the DBAPI, but if
>     Barry> something were to make it into the standard  
> distribution, that's
>     Barry> the direction I'd go in.
>
> I've been working on a lockfile module the past few days on the  
> train.  I
> have something that passes a boatload of doctest test cases on my  
> Mac, works
> for threads as well as processes (acquire the lock in thread one,  
> then block
> in thread two until thread one releases the lock).  The Unix  
> version relies
> on the atomic nature of the link(2) system call.  The Windows  
> version (not
> yet tested on that platform) relies on mkdir(2) being atomic.  (I  
> think it
> is.)  In theory, I suppose the mkdir version will work for both  
> platforms,
> so it's possible that you could have file locking between Windows  
> and Unix
> should you want it.
>
> The current implementation provides a FileLock class with these  
> methods:
>
>     acquire
>     release
>     is_locked
>     break_lock
>     __enter__
>     __exit__
>
> The acquire method takes an optional timeout parameter (None => wait
> forever, +ive value => block for awhile, zero or -ive => give up
> immediately).  The others all take no arguments.  I'm working on ReST
> documentation now and hope to have that finished over the weekend.   
> After
> that I'll write a simple setup.py, toss it out on PyPI, then  
> announce it
> more broadly.  If anyone would like to try it out and/or review the  
> code
> sooner (especially if you have access to Windows) let me know.   
> I'll shoot
> you a copy as it currently exists.
>
> The API and almost all test cases are defined in a _FileLock base  
> class.
> You could (in theory at least) subclass it to provide locking  
> through some
> other shared resource like a database and not have to write and or  
> many
> other test cases.

It sounds pretty interesting Skip, though I won't have time to look  
at that for a couple of weeks.  I did throw mine into PyPI:

http://pypi.python.org/pypi/locknix/1.0

Mostly did it just to get it off my plate.  You're being more  
diligent with tests and documentation than I am, and if your stuff  
works on Windows too, more thorough in your implementation.  There is  
one feature locknix has which probably isn't very interesting to  
other applications, and that is the ability to transfer a lock  
between processes across a fork.

- -Barry

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (Darwin)

iQCVAwUBRyXuiHEjvBPtnXfVAQIEgwP/dn7GXdT5tV3PGpVtU09nkfSPdRTBQQSz
ZZ3wFbSH6J+zMlM+esHLUBm+ZD1cqrlOLLvUIs7J5eRdryF3tRFjbZXqWV+a9/f+
gfLRS0E/80bX5EoJkGesRURtncY00E/cLGdTok4M1ZAWi7oO+K5l812uvrZzblOm
2hb7N/j1zYM=
=0Xq0
-----END PGP SIGNATURE-----


More information about the Python-Dev mailing list