|
5 | 5 | [](https://pypi.python.org/pypi/os-3m-engine)
|
6 | 6 | [](https://pypi.python.org/pypi/os-3m-engine)
|
7 | 7 |
|
| 8 | +Multi-thread engine for 3(or 2) stages job. |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +Although multi-thread of python is buggy and slow, it is still necessary to write multi-thread program. Typically, producer-consumer is the most common model(so called 2-stage job), further more a transporter is needed between them(3-stage job). This library is used to simplify creating 3(or 2) stages program. |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +The 3 stages are: |
| 17 | + |
| 18 | +* Frontend: think as producer, usually used to create or receive data. |
| 19 | +* Transport: receive data from frontend stage, transform and send the transformed data to backend. |
| 20 | +* Backend: think as consumer, process the data received from transport. |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +Something else need to know: |
| 25 | + |
| 26 | +* Each of the stage can be multi-thread. |
| 27 | +* Frontend is required, transport or backend can be omitted. |
| 28 | + |
| 29 | + |
8 | 30 |
|
9 |
| -Multithread engine for 3(or 2) stages job. |
10 |
| - |
11 | 31 |
|
12 | 32 | # Install
|
13 | 33 |
|
14 | 34 | `pip install os-3m-engine`
|
15 | 35 |
|
16 |
| -# Usage |
| 36 | +# API |
| 37 | + |
| 38 | +* Create default engine, a typical 3-stage job: |
| 39 | + |
| 40 | + - frontend: ``os_3m_engine.ootb.StdinFrontend``, read from stdin, send to transport stage |
| 41 | + - transport: ``os_3m_engine.ootb.LogTransport``, log data received from fronted, send to backend stage |
| 42 | + - backend: ``os_3m_engine.ootb.LogBackend``, log data received from transport |
| 43 | + |
| 44 | + ``` |
| 45 | + from os_3m_engine.launcher import create |
| 46 | + |
| 47 | + engine = create() |
| 48 | + ``` |
| 49 | +
|
| 50 | +* Create engine with custom defined stage: |
| 51 | +
|
| 52 | + ``` |
| 53 | + from os_3m_engine.launcher import create |
| 54 | + |
| 55 | + engine = create(transport_cls='transport_class_path_or_class') |
| 56 | + ``` |
| 57 | +
|
| 58 | +* Create engine with custom engine config: |
| 59 | +
|
| 60 | + ``` |
| 61 | + from os_3m_engine.launcher import create |
| 62 | + |
| 63 | + config = WhateverOjbectYouWant |
| 64 | + config.thread_num = 10 |
| 65 | + engine = create(transport_cls='your_transport_cls', engine_transport_config=config) |
| 66 | + ``` |
| 67 | +
|
| 68 | +* Start the engine: |
| 69 | +
|
| 70 | + ``start`` will block the current thread until all of the stage threads stopped |
| 71 | + |
| 72 | + ``` |
| 73 | + engine.start() |
| 74 | + ``` |
| 75 | +
|
| 76 | +* The regular practice of stopping the engine |
| 77 | +
|
| 78 | + ``` |
| 79 | + from signal |
| 80 | + from os_3m_engine.launcher import create |
| 81 | + |
| 82 | + engine = create() |
| 83 | + |
| 84 | + def stop(signum, frame): |
| 85 | + engine.stop() |
| 86 | + |
| 87 | + signal.signal(signal.SIGINT, stop) |
| 88 | + |
| 89 | + engine.start() |
| 90 | + ``` |
| 91 | + |
| 92 | +* Custom frontend class: |
| 93 | +
|
| 94 | + - inherit from ``os_3m_engine.core.frontend.Frontend`` |
| 95 | + - define ``produce`` method as generator |
| 96 | +
|
| 97 | + ``` |
| 98 | + from os_3m_engine.core.frontend import Frontend |
| 99 | + |
| 100 | + class CustomFrontend(Frontend): |
| 101 | + def produce(self): |
| 102 | + yield 'Hello world!' |
| 103 | + ``` |
| 104 | + |
| 105 | +* Custom transport class: |
| 106 | +
|
| 107 | + - inherit from ``os_3m_engine.core.transport.Transport`` |
| 108 | + - define ``transport`` method, the only parameter is the data received, the return value will be sent to backend |
| 109 | +
|
| 110 | + ``` |
| 111 | + from os_3m_engine.core.transport import Transport |
| 112 | + |
| 113 | + class CustomTransport(Transport): |
| 114 | + def transport(self, data): |
| 115 | + return data |
| 116 | + ``` |
| 117 | +
|
| 118 | +* Custom backend class: |
| 119 | +
|
| 120 | + - inherit from ``os_3m_engine.core.backend.Backend`` |
| 121 | + - define ``process`` method, the only parameter is the data received, no need return |
| 122 | +
|
| 123 | + ``` |
| 124 | + from os_3m_engine.core.backend import Backend |
| 125 | + |
| 126 | + class CustomBackend(Backend): |
| 127 | + def process(self, data): |
| 128 | + print(data) |
| 129 | + ``` |
| 130 | +
|
| 131 | +* Passing parameters |
| 132 | +
|
| 133 | + - create engine with custom config object |
| 134 | + - use ``self.config`` to get the config in stage class |
| 135 | +
|
| 136 | + ``` |
| 137 | + from os_3m_engine.launcher import create |
| 138 | + |
| 139 | + config = WhateverOjbectYouWant |
| 140 | + engine = create(app_config=config) |
| 141 | + ``` |
| 142 | +
|
| 143 | +
|
| 144 | +* Custom ``setup``, ``cleanup`` behavior |
| 145 | + |
| 146 | + - each stage class can define ``setup``, ``cleanup`` methods |
| 147 | + - these will be called at each thread start/stop |
| 148 | + |
| 149 | + |
| 150 | + ``` |
| 151 | + from os_3m_engine.core.backend import Backend |
| 152 | + |
| 153 | + class CustomBackend(Backend): |
| 154 | + |
| 155 | + def setup(self): |
| 156 | + print('Setup') |
| 157 | + |
| 158 | + def cleanup(self): |
| 159 | + print('Cleanup') |
| 160 | + |
| 161 | + def process(self, data): |
| 162 | + print(data) |
| 163 | + ``` |
17 | 164 |
|
18 | 165 |
|
19 | 166 |
|
|
0 commit comments