jacinle.utils.filelock#

A platform independent file lock that supports the with-statement.

Original License:

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org>

Module attributes

FileLock

Alias for the lock, which should be used for the current platform.

Classes

BaseFileLock

Implements the base class of a file lock.

FileLock

Alias for the lock, which should be used for the current platform.

SoftFileLock

Simply watches the existence of the lock file.

UnixFileLock

Uses the fcntl.flock() to hard lock the lock file on unix systems.

WindowsFileLock

Uses the msvcrt.locking() function to hard lock the lock file on windows systems.

Exceptions

FileLockTimeout

Raised when the lock could not be acquired in timeout seconds.

Class BaseFileLock

class BaseFileLock[source]#

Bases: object

Implements the base class of a file lock.

__init__(lock_file, timeout=-1)[source]#

Initializes the file lock.

Parameters:
  • lock_file (str) – the path to the lock file.

  • timeout (int) – the timeout in seconds. If -1, there is no timeout.

__new__(**kwargs)#
acquire(timeout=None, poll_intervall=0.05)[source]#

Acquires the file lock or fails with a Timeout error.

Parameters:
  • timeout (float) – the maximum time waited for the file lock. If timeout <= 0, there is no timeout and this method will block until the lock could be acquired. If timeout is None, the default timeout is used.

  • poll_intervall (float) – we check once in poll_intervall seconds if we can acquire the file lock.

# You can use this method in the context manager (recommended)
with lock.acquire():
    pass

# Or you use an equal try-finally construct:
lock.acquire()
try:
    pass
finally:
    lock.release()
release(force=False)[source]#

Releases the file lock.

Please note, that the lock is only completly released, if the lock counter is 0.

Also note, that the lock file itself is not automatically deleted.

Parameters:

force (bool) – If true, the lock counter is ignored and the lock is released in every case.

property is_locked#

True, if the object holds the file lock.

property lock_file#

The path to the lock file.

property timeout#

You can set a default timeout for the filelock. It will be used as fallback value in the acquire method, if no timeout value (None) is given.

If you want to disable the timeout, set it to a negative value.

A timeout of 0 means, that there is exactly one attempt to acquire the file lock.

Class FileLock

FileLock#

Alias for the lock, which should be used for the current platform. On Windows, this is an alias for WindowsFileLock, on Unix for UnixFileLock and otherwise for SoftFileLock.

Class SoftFileLock

class SoftFileLock[source]#

Bases: BaseFileLock

Simply watches the existence of the lock file.

__init__(lock_file, timeout=-1)#

Initializes the file lock.

Parameters:
  • lock_file (str) – the path to the lock file.

  • timeout (int) – the timeout in seconds. If -1, there is no timeout.

__new__(**kwargs)#
acquire(timeout=None, poll_intervall=0.05)#

Acquires the file lock or fails with a Timeout error.

Parameters:
  • timeout (float) – the maximum time waited for the file lock. If timeout <= 0, there is no timeout and this method will block until the lock could be acquired. If timeout is None, the default timeout is used.

  • poll_intervall (float) – we check once in poll_intervall seconds if we can acquire the file lock.

# You can use this method in the context manager (recommended)
with lock.acquire():
    pass

# Or you use an equal try-finally construct:
lock.acquire()
try:
    pass
finally:
    lock.release()
release(force=False)#

Releases the file lock.

Please note, that the lock is only completly released, if the lock counter is 0.

Also note, that the lock file itself is not automatically deleted.

Parameters:

force (bool) – If true, the lock counter is ignored and the lock is released in every case.

property is_locked#

True, if the object holds the file lock.

property lock_file#

The path to the lock file.

property timeout#

You can set a default timeout for the filelock. It will be used as fallback value in the acquire method, if no timeout value (None) is given.

If you want to disable the timeout, set it to a negative value.

A timeout of 0 means, that there is exactly one attempt to acquire the file lock.

Class UnixFileLock

class UnixFileLock[source]#

Bases: BaseFileLock

Uses the fcntl.flock() to hard lock the lock file on unix systems.

__init__(lock_file, timeout=-1)#

Initializes the file lock.

Parameters:
  • lock_file (str) – the path to the lock file.

  • timeout (int) – the timeout in seconds. If -1, there is no timeout.

__new__(**kwargs)#
acquire(timeout=None, poll_intervall=0.05)#

Acquires the file lock or fails with a Timeout error.

Parameters:
  • timeout (float) – the maximum time waited for the file lock. If timeout <= 0, there is no timeout and this method will block until the lock could be acquired. If timeout is None, the default timeout is used.

  • poll_intervall (float) – we check once in poll_intervall seconds if we can acquire the file lock.

# You can use this method in the context manager (recommended)
with lock.acquire():
    pass

# Or you use an equal try-finally construct:
lock.acquire()
try:
    pass
finally:
    lock.release()
release(force=False)#

Releases the file lock.

Please note, that the lock is only completly released, if the lock counter is 0.

Also note, that the lock file itself is not automatically deleted.

Parameters:

force (bool) – If true, the lock counter is ignored and the lock is released in every case.

property is_locked#

True, if the object holds the file lock.

property lock_file#

The path to the lock file.

property timeout#

You can set a default timeout for the filelock. It will be used as fallback value in the acquire method, if no timeout value (None) is given.

If you want to disable the timeout, set it to a negative value.

A timeout of 0 means, that there is exactly one attempt to acquire the file lock.

Class WindowsFileLock

class WindowsFileLock[source]#

Bases: BaseFileLock

Uses the msvcrt.locking() function to hard lock the lock file on windows systems.

__init__(lock_file, timeout=-1)#

Initializes the file lock.

Parameters:
  • lock_file (str) – the path to the lock file.

  • timeout (int) – the timeout in seconds. If -1, there is no timeout.

__new__(**kwargs)#
acquire(timeout=None, poll_intervall=0.05)#

Acquires the file lock or fails with a Timeout error.

Parameters:
  • timeout (float) – the maximum time waited for the file lock. If timeout <= 0, there is no timeout and this method will block until the lock could be acquired. If timeout is None, the default timeout is used.

  • poll_intervall (float) – we check once in poll_intervall seconds if we can acquire the file lock.

# You can use this method in the context manager (recommended)
with lock.acquire():
    pass

# Or you use an equal try-finally construct:
lock.acquire()
try:
    pass
finally:
    lock.release()
release(force=False)#

Releases the file lock.

Please note, that the lock is only completly released, if the lock counter is 0.

Also note, that the lock file itself is not automatically deleted.

Parameters:

force (bool) – If true, the lock counter is ignored and the lock is released in every case.

property is_locked#

True, if the object holds the file lock.

property lock_file#

The path to the lock file.

property timeout#

You can set a default timeout for the filelock. It will be used as fallback value in the acquire method, if no timeout value (None) is given.

If you want to disable the timeout, set it to a negative value.

A timeout of 0 means, that there is exactly one attempt to acquire the file lock.