Skip to main content

Mpcurses is an abstraction of the Python curses and multiprocessing libraries providing function execution and runtime visualization capabilities

Project description

GitHub Workflow Status Code Coverage Code Grade PyPI version

mpcurses

Mpcurses is an abstraction of the Python curses and multiprocessing libraries providing function execution and runtime visualization capabilities at scale. It contains a simple API to enable any Python function to be executed across one or more background processes and includes built-in directives to visualize the functions execution on a terminal screen.

The mpcurses API allows for seamless integration since it does not require the target function to include additional context about curses or multiprocessing. The target function does need to implement logging since log messages are the primary means of inter-process communication between the background processes executing the function and the main process updating the curses screen on the terminal.

The main features are:

  • Execute a function across one or more concurrent processes
  • Queue execution to ensure a predefined number of processes are running
  • Visualize function execution on the terminal screen using curses
  • Define the screen layout using a Python dict
  • Leverage built-in directives to dynamically update the screen when events occur by analyzing log messages
    • Keep numeric counts
    • Update text values and colors
    • Maintain visual indicators
    • Display progress bars
    • Display tables
    • Display lists

Installation

pip install mpcurses

Examples

To run the samples below you need to install the namegenerator module pip install namegenerator

A simple example using mpcurses:

from mpcurses import MPcurses
import namegenerator, time, logging
logger = logging.getLogger(__name__)

def run(*args):
    for _ in range(0, 600):
        logger.debug(f'processing item "{namegenerator.gen()}"')
        time.sleep(.01)

MPcurses(
    function=run,
    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: example

To scale execution of the function across multiple processes, we make a few simple updates:

from mpcurses import MPcurses
import namegenerator, time, logging
logger = logging.getLogger(__name__)

def run(*args):
    group = args[0].get('group', 0)
    for _ in range(0, 600):
        logger.debug(f'processing item "[{group}]: {namegenerator.gen()}"')
        time.sleep(.01)

MPcurses(
    function=run,
    process_data=[{'group': 1}, {'group': 2}, {'group': 3}],
    screen_layout={
        'display_item': {
            'position': (1, 1),
            'color': 14,
            'clear': True,
            'regex': r'^processing item "(?P<value>.*)"$',
            'table': True
        }
    }).execute()

Executing the code above results in the following: example

Serveral examples are included to help introduce the mpcurses library. Note the functions contained in all the examples are Python functions that have no context about multiprocessing or curses, they simply perform a function on a given dataset. Mpcurses takes care of setting up the multiprocessing, configuring the curses screen and maintaining the thread-safe queues that are required for inter-process communication.

example1

Execute a function that processes a list of random items. The screen maintains indicators showing the number of items that have been processed. Two lists are maintained displaying the items that had errors and warnings. example1

example2

Execute a function that processes a list of random items. Execution is scaled across three processes where each is responsible for processing items for a particular group. The screen maintains indicators displaying the items that had errors and warnings for each group. example2

example3

Execute a function that calculates prime numbers for a set range of integers. Execution is scaled across 10 different processes where each process computes the primes on a different set of numbers. For example, the first process computes primes for the set 1-10K, second process 10K-20K, third process 20K-30K, etc. The screen keeps track of the number of prime numbers encountered for each set and maintains a progress bar for each process. example3

Running the examples

Build the Docker image and run the Docker container using the instructions described in the Development section. Run the example scripts within the container:

python examples/example#.py

Projects using mpcurses

Development

Clone the repository and ensure the latest version of Docker is installed on your development server.

Build the Docker image:

docker image build \
--build-arg http_proxy \
--build-arg https_proxy \
-t \
mpcurses:latest .

Run the Docker container:

docker container run \
--rm \
-it \
-e http_proxy \
-e https_proxy \
-v $PWD:/mpcurses \
mpcurses:latest \
/bin/sh

Execute the build:

pyb -X

NOTE: the commands above assume your working behind a http proxy, if that is not the case then the proxy arguments can be discared from both commands.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mpcurses-0.2.5.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

mpcurses-0.2.5-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

Details for the file mpcurses-0.2.5.tar.gz.

File metadata

  • Download URL: mpcurses-0.2.5.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.13

File hashes

Hashes for mpcurses-0.2.5.tar.gz
Algorithm Hash digest
SHA256 4fdd2060ebd948ea7f79c5a7814c923cb24bff2cff228cfe938b175d3cd896ec
MD5 cd8424e6bd29fa952a560aca91b270da
BLAKE2b-256 5be0bda88370ad8eaba2e9f18a13ee3de78c84393658b1dcf7a417423dc53bb1

See more details on using hashes here.

Provenance

File details

Details for the file mpcurses-0.2.5-py3-none-any.whl.

File metadata

  • Download URL: mpcurses-0.2.5-py3-none-any.whl
  • Upload date:
  • Size: 14.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.13

File hashes

Hashes for mpcurses-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 ad405737bb4a24e2dfb247330f283ec851055de59b07c5feb80a49d6b7201203
MD5 b1243a86e599c586ff7a9fb2752af29a
BLAKE2b-256 af89c579ba8e8c1081c6bb134a2f15c09a026d6e885b88bf14ed1df2b8b0fd2e

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page