Skip to content

PythonQt.QtCore.QLockFile

QLockFile

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

Detailed Description

A lock file can be used to prevent multiple processes from accessing concurrently the same resource. For instance, a configuration file on disk, or a socket, a port, a region of shared memory...

Serialization is only guaranteed if all processes that access the shared resource use QLockFile, with the same file path.

QLockFile supports two use cases: to protect a resource for a short-term operation (e.g. verifying if a configuration file has changed before saving new settings), and for long-lived protection of a resource (e.g. a document opened by a user in an editor) for an indefinite amount of time.

When protecting for a short-term operation, it is acceptable to call lock() and wait until any running operation finishes. When protecting a resource over a long time, however, the application should always call setStaleLockTime(0) and then tryLock() with a short timeout, in order to warn the user that the resource is locked.

If the process holding the lock crashes, the lock file stays on disk and can prevent any other process from accessing the shared resource, ever. For this reason, QLockFile tries to detect such a "stale" lock file, based on the process ID written into the file. To cover the situation that the process ID got reused meanwhile, the current process name is compared to the name of the process that corresponds to the process ID from the lock file. If the process names differ, the lock file is considered stale. Additionally, the last modification time of the lock file (30s by default, for the use case of a short-lived operation) is taken into account. If the lock file is found to be stale, it will be deleted.

For the use case of protecting a resource over a long time, you should therefore call setStaleLockTime(0), and when tryLock() returns LockFailedError, inform the user that the document is locked, possibly using getLockInfo() for more details.

Note: On Windows, this class has problems detecting a stale lock if the machine's hostname contains characters outside the US-ASCII character set.

Source: https://doc.qt.io/archives/qt-5.15/qlockfile.html

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
LockError attribute type Convert a number or string to an integer, or return 0 if no arguments are given.
LockFailedError attribute Any
NoError attribute Any (shared captured text)
PermissionError attribute Any
UnknownError attribute Any
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
error function (self) -> Any Returns the lock file error status.
getLockInfo function (self, pid: Any, hostname: Any, appname: Any) -> bool X.getLockInfo(pid, hostname, appname) -> bool
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (shared captured text)
inherits function (self, *args: Any, **kwargs: Any) -> Any Returns if the class inherits or is of given type name (shared captured text)
isLocked function (self) -> bool X.isLocked() -> bool
lock function (self) -> bool X.lock() -> bool
removeStaleLockFile function (self) -> bool X.removeStaleLockFile() -> bool
setStaleLockTime function (self, arg__1: int) -> None Sets staleLockTime to be the time in milliseconds after which a lock file is considered stale.
staleLockTime function (self) -> int X.staleLockTime() -> int
tryLock function (self, timeout: int) -> bool X.tryLock(timeout) -> bool
unlock function (self) -> None Releases the lock, by deleting the lock file.