Skip to main content

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

Project description

mp4ansi

GitHub Workflow Status Code Coverage Code Grade vulnerabilities PyPI version python

A simple ANSI-based terminal emulator that provides multi-processing capabilities. MP4ansi will scale execution of a specified function across multiple background processes, where each process is mapped to specific line on the terminal. As the function executes its log messages will automatically be written to the respective line on the terminal. 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 (as well as the number of lines in the terminal). The result of each function is written to the respective dictionary element and can be interogated upon completion.

MPansi also supports representing the function execution as a progress bar, you will need to provide an optional config argument containing a dictionary for how to query for the total and count (via regular expressions), see the examples for more detail.

MP4ansi is a subclass of mpmq, see the mpmq for more information.

Installation

pip install mp4ansi

Examples

Various examples are included to demonstrate the mp4ansi package. To run the examples, build the Docker image and run the Docker container using the instructions described in the Development section.

MP4ansi

A simple mp4ansi example:

from mp4ansi import MP4ansi
import random, names, logging
logger = logging.getLogger(__name__)

def do_work(*args):
    total = random.randint(50, 100)
    logger.debug(f'processing total of {total}')
    for _ in range(total):
        logger.debug(f'processed {names.get_full_name()}')
    return total

process_data = [{} for item in range(8)]
print('Procesing names...')
MP4ansi(function=do_work, process_data=process_data).execute()
print(f"Total names processed {sum([item['result'] for item in process_data])}")

Executing the code above (example1) results in the following: example

Note the function being executed do_work has no context about multiprocessing or the terminal; it simply perform a function on a given dataset. MP4ansi takes care of setting up the multiprocessing, setting up the terminal, and maintaining the thread-safe queues that are required for inter-process communication.

Let's update the example to add a custom identifer for each process and to show execution as a progress bar. To do this we need to provide additonal configuration via the optional config parameter. Configuration is supplied as a dictionary; id_regex instructs how to query for the identifer from the log messages. For the progress bar, we need to specify total and count_regex to instruct how to query for the total and for when to count that an item has been processed. The value for these settings are specified as regular expressions and will match the function log messages, thus we need to ensure our function has log statements for these. If each instance of your function executes on a static data range then you can specify total as an int, but in this example the data range is dynamic, i.e. each process will execute on varying data ranges.

from mp4ansi import MP4ansi
import names, random, logging
logger = logging.getLogger(__name__)

def do_work(*args):
    logger.debug(f'processor is {names.get_last_name()}')
    total = random.randint(50, 125)
    logger.debug(f'processing total of {total}')
    for _ in range(total):
        logger.debug(f'processed {names.get_full_name()}')
    return total

process_data = [{} for item in range(8)]
config = {
    'id_regex': r'^processor is (?P<value>.*)$',
    'progress_bar': {
        'total': r'^processing total of (?P<value>\d+)$',
        'count_regex': r'^processed (?P<value>.*)$',
        'progress_message': 'Finished processing names'}}
print('Procesing names...')
MP4ansi(function=do_work, process_data=process_data, config=config).execute()
print(f"Total names processed {sum([item['result'] for item in process_data])}")

Executing the code above (example2) results in the following: example

Terminal

The package also exposes a Terminal class if you wish to consume the terminal capabilities without executing background processes. Here is an example for how to do that:

from mp4ansi import Terminal
from essential_generators import DocumentGenerator
import time, random

print('generating random sentences...')
count = 15
docgen = DocumentGenerator()
terminal = Terminal(count)
terminal.write_lines()
terminal.hide_cursor()
for _ in range(800):
    index = random.randint(0, count - 1)
    terminal.write_line(index, docgen.sentence())
    time.sleep(.01)
terminal.write_lines(force=True)
terminal.show_cursor()

Executing the code above (example5) results in the following: example

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 \
mp4ansi:latest .

Run the Docker container:

docker container run \
--rm \
-it \
-v $PWD:/mp4ansi \
mp4ansi: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

mp4ansi-0.3.3.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

mp4ansi-0.3.3-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file mp4ansi-0.3.3.tar.gz.

File metadata

  • Download URL: mp4ansi-0.3.3.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.6.14

File hashes

Hashes for mp4ansi-0.3.3.tar.gz
Algorithm Hash digest
SHA256 0ef260a93d4aa7d59aedabdd834c2d863e7700a82e0c84c353f2fa81a4984708
MD5 e638603fb8643fac464c8fa6eb97f3c4
BLAKE2b-256 dc46250efcc06d8c3ed72e26831262c668c2b6ca9df5d8466e809b48ac12272a

See more details on using hashes here.

Provenance

File details

Details for the file mp4ansi-0.3.3-py3-none-any.whl.

File metadata

  • Download URL: mp4ansi-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.6.14

File hashes

Hashes for mp4ansi-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 847a26ad84c879049bf6cd9226ffc031b61b4b1da7ab0e0e4900aef575aa4e50
MD5 936c4fc3e818f91d1ca08e6b82ed0902
BLAKE2b-256 21a8cdb3dfe036e8af9345a305e1afe6ae825b2576f1b1aa059c5b22fc9efa16

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