def
error
()
def
getLockInfo
(pid, hostname, appname)
def
isLocked
()
def
lock
()
def
removeStaleLockFile
()
def
setStaleLockTime
(arg__1)
def
staleLockTime
()
def
tryLock
([timeout=0])
def
unlock
()
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.
QLockFilesupports 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.当保护短期操作时,可以调用
lock()and wait until any running operation finishes. When protecting a resource over a long time, however, the application should always callsetStaleLockTime(0) 然后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,
QLockFiletries 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.因此,对于长时间保护资源的用例,应该调用
setStaleLockTime(0),和当tryLock()返回LockFailedError,通报用户文档被锁定,可能使用getLockInfo()了解更多细节。注意
On Windows, this class has problems detecting a stale lock if the machine’s hostname contains characters outside the US-ASCII character set.
QLockFile
(
fileName
)
¶
fileName – unicode
PySide2.QtCore.QLockFile.
LockError
¶
此枚举描述最后结果为调用
lock()
or
tryLock()
.
|
常量 |
描述 |
|---|---|
|
QLockFile.NoError |
成功获取锁。 |
|
QLockFile.LockFailedError |
无法获取锁由于被另一进程持有。 |
|
QLockFile.PermissionError |
无法创建锁定文件由于父级目录缺乏权限。 |
|
QLockFile.UnknownError |
发生其它错误,例如:完整分区阻止写出锁定文件。 |
PySide2.QtCore.QLockFile.
getLockInfo
(
pid
,
hostname
,
appname
)
¶
pid
–
qint64
hostname – unicode
appname – unicode
bool
检索锁定文件当前所有者的有关信息。
若
tryLock()
返回
false
,和
error()
返回
LockFailedError
,此函数可以被调用以找出有关现有锁定文件的更多信息:
应用程序的 PID (返回在
pid
)
the
hostname
it’s running on (useful in case of networked filesystems),
the name of the application which created it (returned in
appname
),
注意:
tryLock()
automatically deleted the file if there is no running application with this PID, so
LockFailedError
can only happen if there is an application with this PID (it could be unrelated though).
This can be used to inform users about the existing lock file and give them the choice to delete it. After removing the file using
removeStaleLockFile()
, the application can call
tryLock()
再次。
此函数返回
true
if the information could be successfully retrieved, false if the lock file doesn’t exist or doesn’t contain the expected data. This can happen if the lock file was deleted between the time where
tryLock()
failed and the call to this function. Simply call
tryLock()
again if this happens.
PySide2.QtCore.QLockFile.
lock
(
)
¶
bool
创建锁定文件。
若另一进程 (或另一线程) 已经创建锁定文件,此函数将阻塞直到进程 (或线程) 释放它为止。
不允许在未先解锁的情况下从同一线程同一锁上多次调用此函数。此函数将 dead-lock 当递归锁定文件时。
返回
true
若获得锁,false 若由于不可恢复的错误 (如:父目录没有权限) 而无法获得锁。
PySide2.QtCore.QLockFile.
removeStaleLockFile
(
)
¶
bool
试图强行移除现有锁定文件。
不推荐调用此当保护短期活着操作时:
QLockFile
已经负责移除锁定文件当它们早于
staleLockTime()
.
此方法才应该被调用当保护长时间资源时,即:采用
staleLockTime
(0),和之后
tryLock()
returned
LockFailedError
,且用户同意移除锁定文件。
返回
true
on success, false if the lock file couldn’t be removed. This happens on Windows, when the application owning the lock is still running.
PySide2.QtCore.QLockFile.
setStaleLockTime
(
arg__1
)
¶
arg__1
–
int
集
staleLockTime
to be the time in milliseconds after which a lock file is considered stale. The default value is 30000, i.e. 30 seconds. If your application typically keeps the file locked for more than 30 seconds (for instance while saving megabytes of data for 2 minutes), you should set a bigger value using .
值
staleLockTime
is used by
lock()
and
tryLock()
in order to determine when an existing lock file is considered stale, i.e. left over by a crashed process. This is useful for the case where the PID got reused meanwhile, so one way to detect a stale lock file is by the fact that it has been around for a long time.
另请参阅
PySide2.QtCore.QLockFile.
staleLockTime
(
)
¶
int
返回时间 (以毫秒为单位),在此时间之后锁定文件被认为陈旧。
另请参阅
PySide2.QtCore.QLockFile.
tryLock
(
[
timeout=0
]
)
¶
timeout
–
int
bool
试图创建锁定文件。此函数返回
true
若获得锁;否则它返回
false
. If another process (or another thread) has created the lock file already, this function will wait for at most
timeout
milliseconds for the lock file to become available.
Note: Passing a negative number as the
timeout
is equivalent to calling
lock()
, i.e. this function will wait forever until the lock file can be locked if
timeout
is negative.
If the lock was obtained, it must be released with
unlock()
before another process (or thread) can successfully lock it.
Calling this function multiple times on the same lock from the same thread without unlocking first is not allowed, this function will always return false when attempting to lock the file recursively.
PySide2.QtCore.QLockFile.
unlock
(
)
¶
释放锁,通过删除锁定文件。
Calling without locking the file first, does nothing.