warehut
Installation
pip install warehut
Example
import time
import datetime
import random
from warehut import Warehut
from warehut import Producer
from warehut import Consumer, listen
# There also exists a `Hybrid` worker,
# that can both listen to events and
# when responding, can forward to other consumers.
# Use this with caution and consideration.
from warehut.worker import Worker
class MyWarehut(Warehut):
def handle_error(self, worker_type: type[Worker], exception: Exception):
# Handle exception raised within a worker process
# You could launch a window, write the error to a log file, etc.
# Stopping all other workers
self.stop()
# And printing the error
print(
f'\nWorker of type {worker_type.__name__} '
f'encountered an error.\n{exception!r}\n')
class MyProducer(Producer):
async def __aenter__(self):
print(f'Preparing environment for {self}')
async def __aexit__(self, exc_type, exc_value, trace):
print(f'Gracefully closing environment for {self}')
async def update(self):
# Put whatever you want your producer to do, here.
# It can read from any source and `forward` data to consumer queues.
# This method is run on repeat in-between status checks of the worker.
# Forward the current timestamp to 'ping'
self.forward('ping', time.time())
print('Pinged!')
# Sleep a random amount of time to create obvious offset
time.sleep(random.random() * 2)
# Forward a randomly generated number to 'random'
self.forward('random', random.randint(0, 100))
print('Randomed!')
# Sleep again ... same reason.
time.sleep(random.random() * 2)
class MyConsumer(Consumer):
async def __aenter__(self):
print(f'Preparing environment for {self}')
async def __aexit__(self, exc_type, exc_value, trace):
print(f'Gracefully closing environment for {self}')
# `listen` defines a function to be called with data
# that is labeled with the specified event name.
@listen('ping')
async def on_ping(self, timestamp):
"""Print the time at which a ping was sent"""
timestamp = datetime.datetime.fromtimestamp(int(timestamp))
print(f'Ping at {timestamp.strftime("%Y-%m-%d %H:%M:%S")}')
@listen('random')
async def on_random(self, number):
"""Print generated random numbers"""
if number > 80:
raise RuntimeError('An error to show off the `Warehut` error handler')
print(f'Random number generated: {number}')
if __name__ == '__main__':
with MyWarehut([MyProducer, MyConsumer]):
# `Warehut.start` is called upon entering the context
input('\nPress Enter to exit\n\n')
# `Warehut.stop` is called upon exiting the context
# They can be called on their own with the same effect
#
# Ex.
# warehut = MyWarehut([MyProducer, MyConsumer])
# warehut.start()
# input('\nPress Enter to exit\n\n')
# warehut.stop()
Metadata
Release files for warehut 3.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| warehut-3.4.0.tar.gz | 17.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| warehut-3.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 36.6 kB
Release files / warehut-3.4.0.tar.gz
| Download URL | warehut-3.4.0.tar.gz |
|---|---|
| Size | 17.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
307c4a24cad58461bd1df9b9b68e7a480d821693eac4712893cf81ea9b96dada
|
|
BLAKE2b-256 checksum How to use checksums |
a9e80c09127f605008555fea87286ad3b6cc8db18a3c9e6c84ab27b752a030ac
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.13 CPython/3.10.2 Linux/5.10.102-1-MANJARO
|
Release files / warehut-3.4.0-py3-none-any.whl
| Download URL | warehut-3.4.0-py3-none-any.whl |
|---|---|
| Size | 18.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
98bdb1e8fd6b1917734d40dbca66665b6058526042e8dbaf9c3e6d8428d65bb3
|
|
BLAKE2b-256 checksum How to use checksums |
36c04cc19793a2b7e13d44199be7415c5ee732d0ff2a5cdd6448a73314b228a4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.13 CPython/3.10.2 Linux/5.10.102-1-MANJARO
|