Skip to main content

The mpmq module enables seamless interprocess communication between a parent and child processes when parallelizing a task across multiple workers.

Project description

mpmq

GitHub Workflow Status vulnerabilities coverage complexity PyPI version python

The mpmq module enables seamless interprocess communication between a parent and child processes when parallelizing a task across multiple workers. The MPmq class defines a custom log handler that sends all log messages from child workers to a thread-safe queue that the parent can consume and handle. This is helpful in cases where you want the parent to show real-time progress of child workers as they execute a task.

Installation

pip install mpmq

MPmq class

mpmq.MPmq(function, process_data=None, shared_data=None, processes_to_start=None)

function - the function represents the task you wish the child workers to execute

process_data - list of dictionaries where each dictionary contains the arguments that will be sent to each background child process executing the function; the length of the list dictates the total number of processes that will be executed

shared_data - a dictionary containing arbitrary data that will be sent to all processes as key word arguments

process_to_start - the number of processes to initially start; this represents the number of concurrent processes that will be running. If the total number of processes is greater than this number then execution will be queued and executed to ensure that this concurrency is maintained

execute(raise_if_error=False)

Start execution the process’s activity. If raise_if_error is set to True, an exception will be raised if any function encountered an error during execution.

process_message(offset, message)

Process a message sent from one of the background workers executing the function. The offset represents the index of the executing Process; this number is the same as the corresponding index within the process_data list that was sent to the constructor. The message represents the message that was logged by the function.

Examples

The primary intent is for the MPmq class to be used as a superclass where the subclass ovverrides the process_message method to handle messages coming in from the child workers. The following example demonstrate how this can be done.

Worker Status as a Progress Bar

The example parallizezes a task across multiple processes using a pool of worker processes. Status of each worker is shown as a Progress Bar, as each Child worker in the pool completes an item defined in the task the Parent updates a Progress Bar.

example

Worker Status as a List

The example parallizezes a task across multiple processes using a pool of worker processes. Status of each worker is shown using an array where each index of the array represents an individual worker, as each Child worker in the pool completes the associated item in the List is updated with the completed message.

example

Projects using mpmq

  • mpcurses An abstraction of the Python curses and multiprocessing libraries providing function execution and runtime visualization capabilities

  • mppbars Scale execution of a function across multiple across a number of background processes while displaying their execution status via a progress bar

  • mp4ansi A simple ANSI-based terminal emulator that provides multi-processing capabilities

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 mpmq:latest .

Run the Docker container:

docker container run \
--rm \
-it \
-v $PWD:/code \
mpmq:latest \
bash

Execute the build:

pyb -X

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

mpmq-0.6.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mpmq-0.6.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file mpmq-0.6.0.tar.gz.

File metadata

  • Download URL: mpmq-0.6.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for mpmq-0.6.0.tar.gz
Algorithm Hash digest
SHA256 eeb27d00d1464f0309e91daa95c3b84dd1e0bebb1b8e4d19c3af9427f70b9f46
MD5 116a825b1ad9f8e9c51059ddd168bc07
BLAKE2b-256 fa3f6bd8bc55d26c380a4c23dad09a80abd879562aa699d4e3e1eb3733503834

See more details on using hashes here.

File details

Details for the file mpmq-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: mpmq-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for mpmq-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ad73a32a2b71de03eb9ebb445396b5bf9879a74177ad18ea6f1fc60a56b4b6db
MD5 cfc83390d3fa83ccb5bc0ee5ec5d16ae
BLAKE2b-256 cf48e11b3c8487a54c163477ffe32c4f3429816794f87b5a697f09d554593e3c

See more details on using hashes here.

Supported by

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