Python workers for RichardKnop/machinery
Project description
Machinery Python SDK
This is a subset Python implementation of a worker for RichardKnop/machinery
.
Limitations
- Broker is strictly only for Redis
- BackendResult is strictly only for MongoDB
Development Environment
Use venv
with python 3.
# ---- Setup
$ sudo apt install python3 python3-venv
$ python3 -m venv ~/.venv/simcel
# ---- Activate venv
$ source ~/.venv/simcel/bin/activate
(simcel) $ pip3 install -r requirements.txt
# ---- Running
(simcel) $ python3 main.py
Usage
To start up machinery instance for Python:
from machinery import Value, Machinery
def add(a: Value, b: Value):
return (Value("int64", a.value() + b.value()))
def multiply(a: Value, b: Value):
return (Value("int64", a.value() * b.value()))
def worker(name: Value, delayms: Value):
return (
Value("string", f"hello {name.value()}"),
Value("int64", delayms),
)
machinery = Machinery(
broker_uri="redis://:helloworld@localhost:6379",
backend_uri="mongodb://mongo:moonbucks@localhost:27017/?authSource=admin",
queue="machinery_tasks",
)
machinery.register_callback("add", add)
machinery.register_callback("multiply", multiply)
machinery.register_callback("hello", worker)
# Note that start() is a blocking function.
machinery.start()
To send a machinery task and receive its results:
task = machinery.send_task(queue, "add", [
Value("int64", 30),
Value("int64", 50)
])
# Note that wait_for_results() is a blocking function.
results = machinery.wait_for_results(task)
print(results) # [ Value("int64", 80) ]
Creating a Worker
Workers have these restrictions:
- Inputs should be of type pymachinery.Value
- Outputs should be of type pymachinery.Value, or a tuple of pymachinery.Value
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
pymachinery-0.1.2.tar.gz
(5.5 kB
view hashes)
Built Distribution
Close
Hashes for pymachinery-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1992cf04c333fcaeb51dcddde1a191aa163cbf64499f143c50e2297959c3fd1 |
|
MD5 | 42de05d20b08695de87edbdd66867a8e |
|
BLAKE2b-256 | 0e5a87485a52addb1928f2515c7a9b3086bc12d9e64c574e0505f3701b7f76c2 |