|
4 | 4 | [](https://radon.readthedocs.io/en/latest/api.html#module-radon.complexity)
|
5 | 5 | [](https://pypi.org/project/bandit/)
|
6 | 6 | [](https://badge.fury.io/py/mpcurses)
|
7 |
| -[](https://www.python.org/downloads/) |
| 7 | +[](https://www.python.org/downloads/) |
8 | 8 |
|
9 |
| -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. |
| 9 | +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](https://pypi.org/project/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: |
10 | 10 |
|
11 |
| -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. |
| 11 | +- Numeric counter management |
| 12 | +- Match messages using regular expressions |
| 13 | +- Text value and color updates |
| 14 | +- Visual indicator maintenance |
| 15 | +- Progress bar rendering |
| 16 | +- Table and list displays |
12 | 17 |
|
13 |
| -The main features are: |
14 |
| - |
15 |
| -* Execute a function across one or more concurrent processes |
16 |
| -* Queue execution to ensure a predefined number of processes are running |
17 |
| -* Visualize function execution on the terminal screen using curses |
18 |
| -* Define the screen layout using a Python dict |
19 |
| -* Leverage built-in directives to dynamically update the screen when events occur by analyzing log messages |
20 |
| - * Keep numeric counts |
21 |
| - * Update text values and colors |
22 |
| - * Maintain visual indicators |
23 |
| - * Display progress bars |
24 |
| - * Display tables |
25 |
| - * Display lists |
26 |
| - |
27 |
| - Refer to documentation here: https://soda480.github.io/mpcurses/ |
28 |
| - |
29 |
| - Mpcurses is a subclass of `mpmq`, see [mpmq](https://pypi.org/project/mpmq/) for more information. |
| 18 | + Refer to the MPcurses documentation here: https://soda480.github.io/mpcurses/ |
30 | 19 |
|
31 | 20 | ### Installation
|
32 | 21 | ```bash
|
33 | 22 | pip install mpcurses
|
34 | 23 | ```
|
35 |
| - |
36 | 24 | ### Examples
|
37 | 25 |
|
38 |
| -To run the samples below you need to install the namegenerator module `pip install namegenerator` |
39 |
| - |
40 |
| - |
41 |
| -A simple example using mpcurses: |
| 26 | +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. |
42 | 27 |
|
43 | 28 | ```python
|
44 | 29 | from mpcurses import MPcurses
|
45 | 30 | import namegenerator, time, logging
|
46 | 31 | logger = logging.getLogger(__name__)
|
47 | 32 |
|
48 | 33 | def do_something(*args):
|
49 |
| - for _ in range(0, 600): |
| 34 | + for _ in range(0, 400): |
50 | 35 | logger.debug(f'processing item "{namegenerator.gen()}"')
|
51 | 36 | time.sleep(.01)
|
52 | 37 |
|
53 | 38 | MPcurses(
|
54 | 39 | function=do_something,
|
55 | 40 | screen_layout={
|
56 | 41 | 'display_item': {
|
57 |
| - 'position': (1, 1), |
58 |
| - 'text': 'Processing:', |
59 |
| - 'text_color': 0, |
60 |
| - 'color': 14, |
61 |
| - 'clear': True, |
62 |
| - 'regex': r'^processing item "(?P<value>.*)"$' |
63 |
| - } |
| 42 | + 'position': (1, 1), 'text': 'Processing:', 'text_color': 0, 'color': 14, |
| 43 | + 'clear': True, 'regex': r'^processing item "(?P<value>.*)"$'} |
64 | 44 | }).execute()
|
65 | 45 | ```
|
66 | 46 |
|
67 | 47 | Executing the code above results in the following:
|
68 |
| - |
| 48 | + |
69 | 49 |
|
70 |
| -To scale execution of the function across multiple processes; we set the `process_data` parameter to a list whose total number of elements represent the number of processes to execute, and each list element represent the data to be passed to each respective process: |
71 |
| - |
72 |
| -```python |
73 |
| -from mpcurses import MPcurses |
74 |
| -import namegenerator, time, logging |
75 |
| -logger = logging.getLogger(__name__) |
| 50 | +**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. |
76 | 51 |
|
77 |
| -def do_something(*args): |
78 |
| - group = args[0].get('group', 0) |
79 |
| - for _ in range(0, 600): |
80 |
| - logger.debug(f'processing item "[{group}]: {namegenerator.gen()}"') |
81 |
| - time.sleep(.01) |
| 52 | +Build the Docker image using the instructions below, run the examples. `python examples/##/sample.py` |
82 | 53 |
|
83 |
| -MPcurses( |
84 |
| - function=do_something, |
85 |
| - process_data=[{'group': 1}, {'group': 2}, {'group': 3}], |
86 |
| - screen_layout={ |
87 |
| - 'display_item': { |
88 |
| - 'position': (1, 1), |
89 |
| - 'color': 14, |
90 |
| - 'clear': True, |
91 |
| - 'regex': r'^processing item "(?P<value>.*)"$', |
92 |
| - 'table': True |
93 |
| - } |
94 |
| - }).execute() |
95 |
| -``` |
| 54 | +#### [Prime Numbers Counter](https://github.com/soda480/mpcurses/blob/master/examples/03/sample.py) |
96 | 55 |
|
97 |
| -Executing the code above results in the following: |
98 |
| - |
| 56 | +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. |
99 | 57 |
|
100 |
| -Serveral [examples](https://github.com/soda480/mpcurses/tree/master/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. |
| 58 | + |
101 | 59 |
|
102 |
| -#### [example1](https://github.com/soda480/mpcurses/blob/master/examples/example1.py) |
103 |
| -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. |
104 |
| - |
| 60 | +#### [Item Processor](https://github.com/soda480/mpcurses/blob/master/examples/06/sample.py) |
105 | 61 |
|
106 |
| -#### [example2](https://github.com/soda480/mpcurses/blob/master/examples/example2.py) |
107 |
| -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. |
108 |
| - |
| 62 | +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. |
109 | 63 |
|
110 |
| -#### [example3](https://github.com/soda480/mpcurses/blob/master/examples/example3.py) |
111 |
| -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. |
112 |
| - |
| 64 | + |
113 | 65 |
|
114 |
| -#### Running the examples |
| 66 | +#### [Bay Enclosure Firmware Update](https://github.com/soda480/mpcurses/blob/master/examples/09/sample.py) |
115 | 67 |
|
116 |
| -Build the Docker image and run the Docker container using the instructions described in the [Development](#development) section. Run the example scripts within the container: |
| 68 | +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. |
117 | 69 |
|
118 |
| -```bash |
119 |
| -python examples/example#.py |
120 |
| -``` |
| 70 | + |
121 | 71 |
|
122 | 72 | ### Projects using `mpcurses`
|
123 | 73 |
|
|
0 commit comments