Skip to main content

Mpmq is an abstraction of the Python multiprocessing library providing execution pooling and message queuing capabilities.

Project description

GitHub Workflow Status Code Coverage Code Grade PyPI version

mpmq

Mpmq is an abstraction of the Python multiprocessing library providing execution pooling and message queuing capabilities. Mpmq can scale execution of a specified function across multiple background processes. It creates a log handler that sends all log messages from the running processes to a thread-safe queue. The main process reads the messages off the queue for processing. The number of processes along with the arguments to provide each process is specified as a list of dictionaries. The number of elements in the list will dictate the total number of processes to execute. The result of each function is read from the result queue and written to the respective dictionary element upon completion.

The main features are:

  • execute function across multiple processes
  • queue function execution
  • create log handler that sends function log messages to thread-safe message queue
  • process messages from log message queue
  • maintain result of all executed functions
  • terminate execution using keyboard interrupt

Installation

pip install mpmq

Examples

A simple example using mpmq:

from mpmq import MPmq
import sys, logging
logger = logging.getLogger(__name__)
logging.basicConfig(stream=sys.stdout, level=logging.INFO)

def do_work(*args):
    logger.info(f"hello from process {args[0]['pid']}")
    return 10 + int(args[0]['pid'])

process_data = [{'pid': item} for item in range(3)]
MPmq(function=do_work, process_data=process_data).execute()
print(f"Total items processed {sum([item['result'] for item in process_data])}")

Executing the code above results in the following (for conciseness only INFO level messages are shown):

INFO:mpmq.mpmq:started background process at offset 0 with process id 216
INFO:mpmq.mpmq:started background process at offset 1 with process id 217
INFO:__main__:hello from process 0
INFO:mpmq.mpmq:started background process at offset 2 with process id 218
INFO:mpmq.mpmq:started 3 background processes
INFO:__main__:hello from process 1
INFO:mpmq.mpmq:process at offset 0 process id 216 has completed
INFO:mpmq.mpmq:the to process queue is empty
INFO:__main__:hello from process 2
INFO:mpmq.mpmq:process at offset 1 process id 217 has completed
INFO:mpmq.mpmq:the to process queue is empty
INFO:mpmq.mpmq:process at offset 2 process id 218 has completed
INFO:mpmq.mpmq:the to process queue is empty
INFO:mpmq.mpmq:there are no more active processses - quitting
>>> print(f"Total items processed {sum([item['result'] for item in process_data])}")
Total items processed 33

Projects using mpmq

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

  • 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:/mpmq \
mpmq:latest \
/bin/sh

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.1.3.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

mpmq-0.1.3-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mpmq-0.1.3.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.6.13

File hashes

Hashes for mpmq-0.1.3.tar.gz
Algorithm Hash digest
SHA256 2bd3df7f7e57587dcfcc6ffeb4adc61b4f23f2c16b3238c97f82988c2657bbf5
MD5 1ea7cd6b46e61db048dfb9771381e5bb
BLAKE2b-256 ca78361a15f20c43897bd0f939852a8df93e52464428f5943d4436fea9e919d5

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: mpmq-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.6.13

File hashes

Hashes for mpmq-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9b9d27300d8c5770eee4a90aecd6961075a9439d28af5d14af5acac2d1bbc511
MD5 4a371ed5ee9750d5e5d090ed78b7bcdb
BLAKE2b-256 bbeccffc177416fefb9668821db70a3c36a739375fc24d1004dfdf714317a261

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