portalocker package¶
Submodules¶
portalocker.portalocker module¶
Cross-platform (posix/nt) API for flock-style file locking.
Synopsis:
import portalocker file = open(‘somefile’, ‘r+’) portalocker.lock(file, portalocker.LOCK_EX) file.seek(12) file.write(‘foo’) file.close()
If you know what you’re doing, you may choose to
portalocker.unlock(file)
before closing the file, but why?
Methods:
lock( file, flags ) unlock( file )
Constants:
LOCK_EX LOCK_SH LOCK_NB
Exceptions:
LockException
Notes:
For the ‘nt’ platform, this module requires the Python Extensions for Windows. Be aware that this may not work as expected on Windows 95/98/ME.
History:
I learned the win32 technique for locking files from sample code provided by John Nielsen <nielsenjf@my-deja.com> in the documentation that accompanies the win32 modules.
- Author: Jonathan Feinberg <jdf@pobox.com>,
- Lowell Alleman <lalleman@mfps.com>
Version: $Id: portalocker.py 5474 2008-05-16 20:53:50Z lowell $
-
portalocker.portalocker.
lock
(file_, flags)¶
-
portalocker.portalocker.
unlock
(file_)¶
portalocker.utils module¶
-
class
portalocker.utils.
Lock
(filename, mode='a', truncate=0, timeout=5, check_interval=0.25, fail_when_locked=True, flags=6)[source]¶ Bases:
object
-
portalocker.utils.
open_atomic
(*args, **kwds)[source]¶ Open a file for atomic writing. Instead of locking this method allows you to write the entire file and move it to the actual location. Note that is still not atomic in all cases and won’t work on existing files.
http://docs.python.org/library/os.html#os.rename
>>> filename = 'test_file.txt' >>> if os.path.exists(filename): ... os.remove(filename)
>>> with open_atomic(filename) as fh: ... fh.write('test') >>> assert os.path.exists(filename) >>> os.remove(filename)
Module contents¶
-
portalocker.
lock
(file_, flags)¶
-
portalocker.
unlock
(file_)¶
-
class
portalocker.
Lock
(filename, mode='a', truncate=0, timeout=5, check_interval=0.25, fail_when_locked=True, flags=6)[source]¶ Bases:
object
-
portalocker.
open_atomic
(*args, **kwds)[source]¶ Open a file for atomic writing. Instead of locking this method allows you to write the entire file and move it to the actual location. Note that is still not atomic in all cases and won’t work on existing files.
http://docs.python.org/library/os.html#os.rename
>>> filename = 'test_file.txt' >>> if os.path.exists(filename): ... os.remove(filename)
>>> with open_atomic(filename) as fh: ... fh.write('test') >>> assert os.path.exists(filename) >>> os.remove(filename)