The mpcurses module facilitates seamless terminal screen updates from child processes within a multiprocessing worker pool, leveraging the curses library for terminal manipulation
Project description
mpcurses
The mpcurses package facilitates seamless terminal screen updates from child processes within a multiprocessing worker pool - leveraging the curses library for terminal manipulation. The MPcurses class is a subclass of MPmq; a multiprocessing message queue which enables inter-process communication (IPC) between child workers and a parent process through queuing and consumption of log messages. Mpcurses provides a lightweight abstraction for the curses terminal screen, representing it as a Python dictionary. It includes predefined directives for updating the screen, encompassing:
- Numeric counter management
- Match messages using regular expressions
- Text value and color updates
- Visual indicator maintenance
- Progress bar rendering
- Table and list displays
Refer to the MPcurses documentation here: https://soda480.github.io/mpcurses/
Installation
pip install mpcurses
Examples
Invoke a single child process to execute a task defined by the do_something function. Mpcurses captures all log messages and sends them to a thread-safe queue, the main process consumes messages and uses regular expressions to update the screen which is represented as a dictionary.
from mpcurses import MPcurses
import namegenerator, time, logging
logger = logging.getLogger(__name__)
def do_something(*args):
for _ in range(0, 400):
logger.debug(f'processing item "{namegenerator.gen()}"')
time.sleep(.01)
MPcurses(
function=do_something,
screen_layout={
'display_item': {
'position': (1, 1), 'text': 'Processing:', 'text_color': 0, 'color': 14,
'clear': True, 'regex': r'^processing item "(?P<value>.*)"$'}
}).execute()
Executing the code above results in the following:
NOTE none of the functions being executed in any of the examples include information about the curses screen, multiprocessing or messaging queue - this is handled seamlessly by mpcurses.
Build the Docker image using the instructions below, run the examples. python examples/##/sample.py
Prime Numbers Counter
Execute a function that calculates prime numbers for a set range of integers. Execution is scaled across 7 different workers where each process computes the primes for a different range of numbers. For example, the first worker computes primes for the range 1-10K, second worker computes for the range 10K-20K, etc. The main process keeps track of the number of prime numbers encountered for each worker and shows overall progress for each worker using a progress bar.
Item Processor
Execute a function that processes a list of random items. Execution is scaled across 3 workers where each worker processes a unique set of items. The main process maintains indicators showing the number of items that have been processed by each worker; counting the number of Successful, Errors and Warnings. Three lists are also maintained, one for each group that list which specific items had Warnings and Failures.
Bay Enclosure Firmware Update
Execute a function that contains a workflow containing tasks to update firmware on a server residing in a blade enclosure. Execution is scaled across a worker pool with five active workers. The main process updates the screen showing status of each worker as they execute the workflow tasks for each blade server.
Projects using mpcurses
-
edgexfoundry/sync-github-labels A script that synchronizes GitHub labels and milestones
-
edgexfoundry/prune-github-tags A script that prunes GitHub pre-release tags
Development
Clone the repository and ensure the latest version of Docker is installed on your development server.
Build the Docker image:
docker image build \
-t mpcurses:latest .
Run the Docker container:
docker container run \
--rm \
-it \
-v $PWD:/code \
mpcurses:latest \
bash
Execute the build:
pyb -X
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 mpcurses-1.0.0.tar.gz.
File metadata
- Download URL: mpcurses-1.0.0.tar.gz
- Upload date:
- Size: 12.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e8cd751d1aca208a939a45cf7d92f6464a9363676af856dc11c7d6e7ccfdfdf6
|
|
| MD5 |
51b0c766a2efa4ed5acd32464bf05797
|
|
| BLAKE2b-256 |
c580185a57da6b63740051f3240f1361359139b01b8d8a17f59f2265ed8271ed
|
File details
Details for the file mpcurses-1.0.0-py3-none-any.whl.
File metadata
- Download URL: mpcurses-1.0.0-py3-none-any.whl
- Upload date:
- Size: 11.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b062ab0f50ce8a87b77955ebe8b1823c5aabb1dd92d56dd29ae06a33d9749e0
|
|
| MD5 |
f83b8e9f451afccdffa4102804131bbf
|
|
| BLAKE2b-256 |
d2d5e5320af83ba41bf2636ea79f015fbbd9b1c1cf1410ce40cb07ba6e37c97c
|