QSharedMemory#
The QSharedMemory
class provides access to a shared memory segment. More…
Synopsis#
Functions#
def
attach
([mode=QSharedMemory.AccessMode.ReadWrite])def
constData
()def
create
(size[, mode=QSharedMemory.AccessMode.ReadWrite])def
detach
()def
error
()def
errorString
()def
isAttached
()def
key
()def
lock
()def
nativeIpcKey
()def
nativeKey
()def
setKey
(key)def
setNativeKey
(key)def
setNativeKey
(key[, type=QNativeIpcKey.legacyDefaultTypeForOs()])def
size
()def
unlock
()
Static functions#
def
isKeyTypeSupported
(type)def
legacyNativeKey
(key[, type=QNativeIpcKey.legacyDefaultTypeForOs()])def
platformSafeKey
(key[, type=QNativeIpcKey.DefaultTypeForOs])
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description#
QSharedMemory
provides access to a shared memory segment by multiple threads and processes. Shared memory segments are identified by a key, represented by QNativeIpcKey
. A key can be created in a cross-platform manner by using platformSafeKey().
One QSharedMemory
object must create()
the segment and this call specifies the size of the segment. All other processes simply attach()
to the segment that must already exist. After either operation is successful, the application may call data()
to obtain a pointer to the data.
To support non-atomic operations, QSharedMemory
provides API to gain exclusive access: you may lock the shared memory with lock()
before reading from or writing to the shared memory, but remember to release the lock with unlock()
after you are done.
By default, QSharedMemory
automatically destroys the shared memory segment when the last instance of QSharedMemory
is detached
from the segment, and no references to the segment remain.
For details on the key types, platform-specific limitations, and interoperability with older or non-Qt applications, see the Native IPC Keys documentation. That includes important information for sandboxed applications on Apple platforms, including all apps obtained via the Apple App Store.
- class PySide6.QtCore.QSharedMemory([parent=None])#
PySide6.QtCore.QSharedMemory(key[, parent=None])
PySide6.QtCore.QSharedMemory(key[, parent=None])
- Parameters:
parent –
PySide6.QtCore.QObject
This function overloads QSharedMemory()
.
Constructs a shared memory object with the given parent
. The shared memory object’s key is not set by the constructor, so the shared memory object does not have an underlying shared memory segment attached. The key must be set with setNativeKey()
before create()
or attach()
can be used.
See also
This is an overloaded function.
Constructs a shared memory object with the given parent
and with its key set to key
. Because its key is set, its create()
and attach()
functions can be called.
See also
Constructs a shared memory object with the given parent
and with the legacy key set to key
. Because its key is set, its create()
and attach()
functions can be called.
- PySide6.QtCore.QSharedMemory.AccessMode#
Constant
Description
QSharedMemory.ReadOnly
The shared memory segment is read-only. Writing to the shared memory segment is not allowed. An attempt to write to a shared memory segment created with ReadOnly causes the program to abort.
QSharedMemory.ReadWrite
Reading and writing the shared memory segment are both allowed.
- PySide6.QtCore.QSharedMemory.SharedMemoryError#
Constant
Description
QSharedMemory.NoError
No error occurred.
QSharedMemory.PermissionDenied
The operation failed because the caller didn’t have the required permissions.
QSharedMemory.InvalidSize
A create operation failed because the requested size was invalid.
QSharedMemory.KeyError
The operation failed because of an invalid key.
QSharedMemory.AlreadyExists
A
create()
operation failed because a shared memory segment with the specified key already existed.QSharedMemory.NotFound
An
attach()
failed because a shared memory segment with the specified key could not be found.QSharedMemory.LockError
The attempt to
lock()
the shared memory segment failed becausecreate()
orattach()
failed and returned false, or because a system error occurred inacquire()
.QSharedMemory.OutOfResources
A
create()
operation failed because there was not enough memory available to fill the request.QSharedMemory.UnknownError
Something else happened and it was bad.
- PySide6.QtCore.QSharedMemory.attach([mode=QSharedMemory.AccessMode.ReadWrite])#
- Parameters:
mode –
AccessMode
- Return type:
bool
Attempts to attach the process to the shared memory segment identified by the key that was passed to the constructor or to a call to setNativeKey()
. The access mode
is ReadWrite
by default. It can also be ReadOnly
. Returns true
if the attach operation is successful. If false is returned, call error()
to determine which error occurred. After attaching the shared memory segment, a pointer to the shared memory can be obtained by calling data()
.
See also
- PySide6.QtCore.QSharedMemory.constData()#
- Return type:
void
Returns a const pointer to the contents of the shared memory segment, if one is attached. Otherwise it returns null. The value returned by this function will not change until a detach
happens, so it is safe to store this pointer.
If the memory operations are not atomic, you may lock the shared memory with lock()
before reading from or writing, but remember to release the lock with unlock()
after you are done.
- PySide6.QtCore.QSharedMemory.create(size[, mode=QSharedMemory.AccessMode.ReadWrite])#
- Parameters:
size – int
mode –
AccessMode
- Return type:
bool
Creates a shared memory segment of size
bytes with the key passed to the constructor or set with setNativeKey()
, then attaches to the new shared memory segment with the given access mode
and returns true
. If a shared memory segment identified by the key already exists, the attach operation is not performed and false
is returned. When the return value is false
, call error()
to determine which error occurred.
See also
- PySide6.QtCore.QSharedMemory.detach()#
- Return type:
bool
Detaches the process from the shared memory segment. If this was the last process attached to the shared memory segment, then the shared memory segment is released by the system, i.e., the contents are destroyed. The function returns true
if it detaches the shared memory segment. If it returns false
, it usually means the segment either isn’t attached, or it is locked by another process.
See also
- PySide6.QtCore.QSharedMemory.error()#
- Return type:
Returns a value indicating whether an error occurred, and, if so, which error it was.
See also
- PySide6.QtCore.QSharedMemory.errorString()#
- Return type:
str
Returns a text description of the last error that occurred. If error()
returns an error value
, call this function to get a text string that describes the error.
See also
- PySide6.QtCore.QSharedMemory.isAttached()#
- Return type:
bool
Returns true
if this process is attached to the shared memory segment.
- static PySide6.QtCore.QSharedMemory.isKeyTypeSupported(type)#
- Parameters:
type –
Type
- Return type:
bool
- PySide6.QtCore.QSharedMemory.key()#
- Return type:
str
Returns the legacy key assigned with setKey()
to this shared memory, or a null key if no key has been assigned, or if the segment is using a nativeKey()
. The key is the identifier used by Qt applications to identify the shared memory segment.
You can find the native, platform specific, key used by the operating system by calling nativeKey()
.
See also
- static PySide6.QtCore.QSharedMemory.legacyNativeKey(key[, type=QNativeIpcKey.legacyDefaultTypeForOs()])#
- Parameters:
key – str
type –
Type
- Return type:
- PySide6.QtCore.QSharedMemory.lock()#
- Return type:
bool
This is a semaphore that locks the shared memory segment for access by this process and returns true
. If another process has locked the segment, this function blocks until the lock is released. Then it acquires the lock and returns true
. If this function returns false
, it means that you have ignored a false return from create()
or attach()
, that you have set the key with setNativeKey()
or that acquire()
failed due to an unknown system error.
- PySide6.QtCore.QSharedMemory.nativeIpcKey()#
- Return type:
Returns the key type for this shared memory object. The key type complements the nativeKey()
as the identifier used by the operating system to identify the shared memory segment.
You can use the native key to access shared memory segments that have not been created by Qt, or to grant shared memory access to non-Qt applications. See Native IPC Keys for more information.
See also
- PySide6.QtCore.QSharedMemory.nativeKey()#
- Return type:
str
Returns the native, platform specific, key for this shared memory object. The native key is the identifier used by the operating system to identify the shared memory segment.
You can use the native key to access shared memory segments that have not been created by Qt, or to grant shared memory access to non-Qt applications. See Native IPC Keys for more information.
See also
- static PySide6.QtCore.QSharedMemory.platformSafeKey(key[, type=QNativeIpcKey.DefaultTypeForOs])#
- Parameters:
key – str
type –
Type
- Return type:
- PySide6.QtCore.QSharedMemory.setKey(key)#
- Parameters:
key – str
This is an overloaded function.
Sets the legacy key
for this shared memory object. If key
is the same as the current key, the function returns without doing anything. Otherwise, if the shared memory object is attached to an underlying shared memory segment, it will detach
from it before setting the new key. This function does not do an attach()
.
You can call key()
to retrieve the legacy key. This function is mostly the same as:
shm.setNativeKey(QSharedMemory::legacyNativeKey(key));
except that it enables obtaining the legacy key using key()
.
See also
- PySide6.QtCore.QSharedMemory.setNativeKey(key)#
- Parameters:
Sets the native, platform specific, key
for this shared memory object. If key
is the same as the current native key, the function returns without doing anything. Otherwise, if the shared memory object is attached to an underlying shared memory segment, it will detach
from it before setting the new key. This function does not do an attach()
.
This function is useful if the native key was shared from another process. See Native IPC Keys for more information.
Portable native keys can be obtained using platformSafeKey().
You can call nativeKey()
to retrieve the native key.
See also
- PySide6.QtCore.QSharedMemory.setNativeKey(key[, type=QNativeIpcKey.legacyDefaultTypeForOs()])
- Parameters:
key – str
type –
Type
Sets the native, platform specific, key
for this shared memory object of type type
(the type parameter has been available since Qt 6.6). If key
is the same as the current native key, the function returns without doing anything. Otherwise, if the shared memory object is attached to an underlying shared memory segment, it will detach
from it before setting the new key. This function does not do an attach()
.
This function is useful if the native key was shared from another process, though the application must take care to ensure the key type matches what the other process expects. See Native IPC Keys for more information.
Portable native keys can be obtained using platformSafeKey().
You can call nativeKey()
to retrieve the native key.
See also
- PySide6.QtCore.QSharedMemory.size()#
- Return type:
int
Returns the size of the attached shared memory segment. If no shared memory segment is attached, 0 is returned.
Note
The size of the segment may be larger than the requested size that was passed to create()
.
- PySide6.QtCore.QSharedMemory.unlock()#
- Return type:
bool
Releases the lock on the shared memory segment and returns true
, if the lock is currently held by this process. If the segment is not locked, or if the lock is held by another process, nothing happens and false is returned.
See also