Skip to main content
# os-m3-engine

[![Build Status](https://www.travis-ci.org/cfhamlet/os-m3-engine.svg?branch=master)](https://www.travis-ci.org/cfhamlet/os-m3-engine)
[![codecov](https://codecov.io/gh/cfhamlet/os-m3-engine/branch/master/graph/badge.svg)](https://codecov.io/gh/cfhamlet/os-m3-engine)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/os-m3-engine.svg)](https://pypi.python.org/pypi/os-m3-engine)
[![PyPI](https://img.shields.io/pypi/v/os-m3-engine.svg)](https://pypi.python.org/pypi/os-m3-engine)

Multi-thread engine for 3(or 2) stages job.



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.



The 3 stages are:

* Frontend: think as producer, usually used to create or receive data.
* Transport: receive data from frontend stage, transform and send the transformed data to backend.
* Backend: think as consumer, process the data received from transport.



Something else need to know:

* Each of the stage can be multi-thread.
* Frontend is required, transport or backend can be omitted.




# Install

`pip install os-m3-engine`

# API

* Create default engine, a typical 3-stage job:

- frontend: ``os_m3_engine.ootb.StdinFrontend``, read from stdin, send to transport stage
- transport: ``os_m3_engine.ootb.LogTransport``, log data received from fronted, send to backend stage
- backend: ``os_m3_engine.ootb.LogBackend``, log data received from transport

```
from os_m3_engine.launcher import create

engine = create()
```

* Create engine with custom defined stage:

```
from os_m3_engine.launcher import create

engine = create(transport_cls='transport_class_path_or_class')
```

* Create engine with custom engine config:

```
from os_m3_engine.launcher import create

config = WhateverOjbectYouWant
config.thread_num = 10
engine = create(transport_cls='your_transport_cls', engine_transport_config=config)
```

* Start the engine:

``start`` will block the current thread until all of the stage threads stopped

```
engine.start()
```

* The regular practice of stopping the engine

```
from signal
from os_m3_engine.launcher import create

engine = create()

def stop(signum, frame):
engine.stop()

signal.signal(signal.SIGINT, stop)

engine.start()
```

* Custom frontend class:

- inherit from ``os_m3_engine.core.frontend.Frontend``
- define ``produce`` method as generator

```
from os_m3_engine.core.frontend import Frontend

class CustomFrontend(Frontend):
def produce(self):
yield 'Hello world!'
```

* Custom transport class:

- inherit from ``os_m3_engine.core.transport.Transport``
- define ``transport`` method, the only parameter is the data received, the return value will be sent to backend

```
from os_m3_engine.core.transport import Transport

class CustomTransport(Transport):
def transport(self, data):
return data
```

* Custom backend class:

- inherit from ``os_m3_engine.core.backend.Backend``
- define ``process`` method, the only parameter is the data received, no need return

```
from os_m3_engine.core.backend import Backend

class CustomBackend(Backend):
def process(self, data):
print(data)
```

* Passing parameters

- create engine with custom config object
- use ``self.config`` to get the config in stage class

```
from os_m3_engine.launcher import create

config = WhateverOjbectYouWant
engine = create(app_config=config)
```


* Custom ``setup``, ``cleanup`` behavior

- each stage class can define ``setup``, ``cleanup`` methods
- these will be called at each thread start/stop


```
from os_m3_engine.core.backend import Backend

class CustomBackend(Backend):

def setup(self):
print('Setup')

def cleanup(self):
print('Cleanup')

def process(self, data):
print(data)
```



# Unit Tests

`$ tox`

# License

MIT licensed.

Metadata

Release files for os-m3-engine 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for os-m3-engine 0.1.2
File Size Uploaded
os-m3-engine-0.1.2.tar.gz 10.4 kB Details

Release files / os-m3-engine-0.1.2.tar.gz

Download URL os-m3-engine-0.1.2.tar.gz
Size 10.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d49b87cbe73808c295968ca7728e2b3a6bcc5dc88546f480400a16e1f7ab6059
BLAKE2b-256 checksum
How to use checksums
430192673cc9c3ce4ac1b8782457e632fc72bd9104f806030c0c9c215945dce0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/2.7.14

Release history Release notifications | RSS feed

This release

0.1.2 This release

1 release file

0.1.1

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page