QSystemSemaphoreclass provides a general counting system semaphore. 更多 …
信号量是互斥的一般化。互斥只可以锁定一次,而信号量可以多次获取。通常,信号量用于保护一定数量的恒等资源。
像其更轻搭档
QSemaphore,QSystemSemaphorecan be accessed from multiplethreads。不像QSemaphore,QSystemSemaphorecan also be accessed from multipleprocesses. This meansQSystemSemaphoreis a much heavier class, so if your application doesn’t need to access your semaphores across multiple processes, you will probably want to useQSemaphore.信号量支持 2 基础操作
acquire()andrelease():
acquire()tries to acquire one resource. If there isn’t a resource available, the call blocks until a resource becomes available. Then the resource is acquired and the call returns.
release()releases one resource so it can be acquired by another process. The function can also be called with a parameter n > 1, which releases n resources.A system semaphore is created with a string key that other processes can use to use the same semaphore.
范例:创建系统信号量
sem = QSystemSemaphore("market", 3, QSystemSemaphore.Create) # resources available == 3 sem.acquire() # resources available == 2 sem.acquire() # resources available == 1 sem.acquire() # resources available == 0 sem.release() # resources available == 1 sem.release(2) # resources available == 3A typical application of system semaphores is for controlling access to a circular buffer shared by a producer process and a consumer processes.
当使用此类时,要意识到以下平台差异:
Windows:
QSystemSemaphoredoes not own its underlying system semaphore. Windows owns it. This means that when all instances ofQSystemSemaphorefor a particular key have been destroyed, either by having their destructors called, or because one or more processes crash, Windows removes the underlying system semaphore.Unix:
QSystemSemaphoreowns the underlying system semaphore in Unix systems. This means that the last process having an instance ofQSystemSemaphorefor a particular key must remove the underlying system semaphore in its destructor. If the last process crashes without running theQSystemSemaphoredestructor, Unix does not automatically remove the underlying system semaphore, and the semaphore survives the crash. A subsequent process that constructs aQSystemSemaphorewith the same key will then be given the existing system semaphore. In that case, if theQSystemSemaphoreconstructor has specified itsaccess modeas打开, its initial resource count will not be reset to the one provided but remain set to the value it received in the crashed process. To protect against this, the first process to create a semaphore for a particular key (usually a server), must pass itsaccess modeas创建, which will force Unix to reset the resource count in the underlying system semaphore.When a process using
QSystemSemaphoreterminates for any reason, Unix automatically reverses the effect of all acquire operations that were not released. Thus if the process acquires a resource and then exits without releasing it, Unix will release that resource.另请参阅
QSharedMemoryQSemaphore
QSystemSemaphore
(
key
[
,
initialValue=0
[
,
mode=Open
]
]
)
¶
- param initialValue
int- param key
unicode
- param mode
AccessMode
Requests a system semaphore for the specified
key
。参数
initialValue
and
mode
are used according to the following rules, which are system dependent.
In Unix, if the
mode
is
打开
and the system already has a semaphore identified by
key
, that semaphore is used, and the semaphore’s resource count is not changed, i.e.,
initialValue
is ignored. But if the system does not already have a semaphore identified by
key
, it creates a new semaphore for that key and sets its resource count to
initialValue
.
In Unix, if the
mode
is
创建
and the system already has a semaphore identified by
key
, that semaphore is used, and its resource count is set to
initialValue
. If the system does not already have a semaphore identified by
key
, it creates a new semaphore for that key and sets its resource count to
initialValue
.
In Windows,
mode
is ignored, and the system always tries to create a semaphore for the specified
key
. If the system does not already have a semaphore identified as
key
, it creates the semaphore and sets its resource count to
initialValue
. But if the system already has a semaphore identified as
key
it uses that semaphore and ignores
initialValue
.
mode
parameter is only used in Unix systems to handle the case where a semaphore survives a process crash. In that case, the next process to allocate a semaphore with the same
key
will get the semaphore that survived the crash, and unless
mode
is
创建
, the resource count will not be reset to
initialValue
but will retain the initial value it had been given by the crashed process.
PySide2.QtCore.QSystemSemaphore.
AccessMode
¶
This enum is used by the constructor and
setKey()
. Its purpose is to enable handling the problem in Unix implementations of semaphores that survive a crash. In Unix, when a semaphore survives a crash, we need a way to force it to reset its resource count, when the system reuses the semaphore. In Windows, where semaphores can’t survive a crash, this enum has no effect.
|
常量 |
描述 |
|---|---|
|
QSystemSemaphore.Open |
If the semaphore already exists, its initial resource count is not reset. If the semaphore does not already exist, it is created and its initial resource count set. |
|
QSystemSemaphore.Create |
|
PySide2.QtCore.QSystemSemaphore.
SystemSemaphoreError
¶
|
常量 |
描述 |
|---|---|
|
QSystemSemaphore.NoError |
没有发生错误。 |
|
QSystemSemaphore.PermissionDenied |
The operation failed because the caller didn’t have the required permissions. |
|
QSystemSemaphore.KeyError |
操作失败,因为键无效。 |
|
QSystemSemaphore.AlreadyExists |
The operation failed because a system semaphore with the specified key already existed. |
|
QSystemSemaphore.NotFound |
The operation failed because a system semaphore with the specified key could not be found. |
|
QSystemSemaphore.OutOfResources |
The operation failed because there was not enough memory available to fill the request. |
|
QSystemSemaphore.UnknownError |
发生其它事情且很糟糕。 |
PySide2.QtCore.QSystemSemaphore.
acquire
(
)
¶
bool
Acquires one of the resources guarded by this semaphore, if there is one available, and returns
true
. If all the resources guarded by this semaphore have already been acquired, the call blocks until one of them is released by another process or thread having a semaphore with the same key.
If false is returned, a system error has occurred. Call
error()
to get a value of
SystemSemaphoreError
that indicates which error occurred.
另请参阅
PySide2.QtCore.QSystemSemaphore.
error
(
)
¶
返回指示是否发生错误的值,且若如此,指示错误是什么。
另请参阅
PySide2.QtCore.QSystemSemaphore.
errorString
(
)
¶
unicode
返回最后发生错误的文本描述。若
error()
返回
error
value
,调用此函数以获取描述错误的文本字符串。
另请参阅
PySide2.QtCore.QSystemSemaphore.
key
(
)
¶
unicode
Returns the key assigned to this system semaphore. The key is the name by which the semaphore can be accessed from other processes.
另请参阅
PySide2.QtCore.QSystemSemaphore.
release
(
[
n=1
]
)
¶
n
–
int
bool
发行
n
resources guarded by the semaphore. Returns
true
unless there is a system error.
Example: Create a system semaphore having five resources; acquire them all and then release them all.
sem = QSystemSemaphore("market", 5, QSystemSemaphore.Create)
sem.acquire(5) # acquire all 5 resources
sem.release(5) # release the 5 resources
This function can also “create” resources. For example, immediately following the sequence of statements above, suppose we add the statement:
sem.release(10) # "create" 10 new resources
Ten new resources are now guarded by the semaphore, in addition to the five that already existed. You would not normally use this function to create more resources.
另请参阅
PySide2.QtCore.QSystemSemaphore.
setKey
(
key
[
,
initialValue=0
[
,
mode=Open
]
]
)
¶
key – unicode
initialValue
–
int
mode
–
AccessMode
This function works the same as the constructor. It reconstructs this
QSystemSemaphore
object. If the new
key
is different from the old key, calling this function is like calling the destructor of the semaphore with the old key, then calling the constructor to create a new semaphore with the new
key
。
initialValue
and
mode
parameters are as defined for the constructor.
另请参阅
QSystemSemaphore()
key()