IntEnumValue type for Python multiprocessing
Project description
Python multiprocessing-intenum
IntEnumValue is an implementation of a multiprocessing safe shared object
for IntEnum enum values.
The IntEnumValue appears and can be used much like an IntEnum, while
internally, it uses a multiprocessing.Value shared ctypes integer object
to store the integer value of the enum in a multiprocessing safe way.
It is fully typed and implemented as a generic class, thus allowing to guard
against programming errors where values other than the designated
specific IntEnum type are being assigned.
License
Copyright (C) 2024-2025 credativ GmbH https://www.credativ.de/en/
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Installation
- Install required packages, e.g. on Debian GNU/Linux:
apt-get install git python3
-
Install uv from https://github.com/astral-sh/uv/releases
-
Setup and activate Python virtual environment:
uv venv --prompt='mp-intenum' . .venv/bin/activate -
Install Python requirements in the virtual environment::
uv sync --locked -
Build:
make dist
Development
- Run code quality checks:
make lint
- Run test suite:
make test
Usage
Example
To illustrate the usage, an example of a multithreaded worker is used, which has an internal state represented as an enum that should be tracked across its threads.
To use a multiprocessing safe IntEnum, first define the actual underlying
IntEnum type:
class WorkerStatusEnum(IntEnum):
UNAVAILABLE = enum.auto()
IDLE = enum.auto()
CALCULATING = enum.auto()
IntEnumValue is a generic class, accepting a type parameter for the type
of the underlying IntEnum.
Thus define the corresponding IntEnumValue type for that IntEnum like
this:
class WorkerStatus(IntEnumValue[WorkerStatusEnum]):
pass
Now this WorkerStatus type can be used like an enum to track the state:
>>> status = WorkerStatus(WorkerStatusEnum.IDLE)
>>> status.name
'IDLE'
>>> status.value
2
>>> with status.get_lock():
... status.set(WorkerStatusEnum.CALCULATING)
>>> status.name
'CALCULATING'
>>> status.value
3
It can, of course, also be wrapped in a dedicated class, which the remaining examples will further expand on:
class WorkerState:
def __init__(self) -> None:
self.status = WorkerStatus(WorkerStatusEnum.IDLE)
When using multiple multiprocessing.Value instances (including
IntEnumValue ones) that should share a lock to allow ensuring that they
can only be changed in a consistent state, pass that shared lock as a
keyword argument on instantiation:
class WorkerState:
def __init__(self) -> None:
self.lock = multiprocessing.RLock()
self.status = WorkerStatus(WorkerStatusEnum.IDLE, lock=self.lock)
self.job_id = multiprocessing.Value("i", -1, lock=self.lock)
def get_lock(self) -> Lock | RLock:
return self.lock
To avoid having to call the set() method to assign a value to the
IntEnumValue attribute, it is suggested to keep the actual
attribute private to the class and implement getter and setter methods for a
public property that hides this implementation detail, e.g. as follows:
class WorkerState:
def __init__(self) -> None:
self._status = WorkerStatus(WorkerStatusEnum.IDLE)
@property
def status(self) -> WorkerStatusEnum:
return self._status # type: ignore[return-value]
@status.setter
def status(self, status: WorkerStatusEnum | str) -> None:
self._status.set(status)
This can be used in a more elegant manner by simply assigning to the status attribute:
>>> state = WorkerState()
>>> state.status.name
'IDLE'
>>> with state.get_lock():
... state.status = WorkerStatusEnum.CALCULATING
>>> state.status.name
'CALCULATING'
The specific IntEnumValue type can override methods to add further
functionality.
A common example is overriding the set() method to add logging:
class WorkerStatus(IntEnumValue[WorkerStatusEnum]):
def set(self, value: WorkerStatusEnum | str) -> None:
super().set(value)
logger.info(f"WorkerStatus set to '{self.name}'")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file multiprocessing_intenum-1.0.tar.gz.
File metadata
- Download URL: multiprocessing_intenum-1.0.tar.gz
- Upload date:
- Size: 42.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5984c499747accfd8b323587db71012d48cc10e445b0e69b3d08697ca7f6cb43
|
|
| MD5 |
528cc5f778fac6bc5e225f561fa9b4a2
|
|
| BLAKE2b-256 |
44a70ed243742a18e2596369e71a1ced793f76250248e228ddc3849bc5bcf649
|
Provenance
The following attestation bundles were made for multiprocessing_intenum-1.0.tar.gz:
Publisher:
workflow.yml on credativ/python-multiprocessing-intenum
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
multiprocessing_intenum-1.0.tar.gz -
Subject digest:
5984c499747accfd8b323587db71012d48cc10e445b0e69b3d08697ca7f6cb43 - Sigstore transparency entry: 828765629
- Sigstore integration time:
-
Permalink:
credativ/python-multiprocessing-intenum@0b8161cbc78e930c4575b2d2b6f5255b631c2a9f -
Branch / Tag:
refs/tags/v1.0 - Owner: https://github.com/credativ
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@0b8161cbc78e930c4575b2d2b6f5255b631c2a9f -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file multiprocessing_intenum-1.0-py3-none-any.whl.
File metadata
- Download URL: multiprocessing_intenum-1.0-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c41621a0f24c8be6af3ecb6f692914c5c1ae936ba24ec9156847fb1b95e85a24
|
|
| MD5 |
391eb48c9e697452cc4cbdd4168de3d2
|
|
| BLAKE2b-256 |
df260fd5fe1f15a996470c7fbe919b2c4aa7a48cb476b0d48234c388d3760732
|
Provenance
The following attestation bundles were made for multiprocessing_intenum-1.0-py3-none-any.whl:
Publisher:
workflow.yml on credativ/python-multiprocessing-intenum
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
multiprocessing_intenum-1.0-py3-none-any.whl -
Subject digest:
c41621a0f24c8be6af3ecb6f692914c5c1ae936ba24ec9156847fb1b95e85a24 - Sigstore transparency entry: 828765683
- Sigstore integration time:
-
Permalink:
credativ/python-multiprocessing-intenum@0b8161cbc78e930c4575b2d2b6f5255b631c2a9f -
Branch / Tag:
refs/tags/v1.0 - Owner: https://github.com/credativ
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@0b8161cbc78e930c4575b2d2b6f5255b631c2a9f -
Trigger Event:
workflow_dispatch
-
Statement type: