A producer-customer model based on redis
Project description
Python producer-customer model based on redis
Installment
use pip:
pip install woodenwaiter
from source code:
python3 setup.py install
Run Example
python3 -m woodenwaiter.woodenwaiter
Example Usage
A simple program that showing how to use WoodenWaiter as a customer and producer
customer example:
import time
from woodenwaiter.woodenwaiter import WoodenWaiter
from woodenwaiter.woodenwaiter import WoodenCustomer
from woodenwaiter.woodenwaiter import WoodenManager
if __name__ == '__main__':
table1 = 'cmdb'
table2 = 'rbac'
dish1 = 'custom_sync'
dish2 = 'some_task'
waiter = WoodenWaiter()
def print_foods(foods):
print('custom foods')
print('foods is: {}'.format(foods))
customer1 = WoodenCustomer(
table=table1, dish=dish1, waiter=waiter,
process=print_foods, seconds=1)
customer2 = WoodenCustomer(
table=table2, dish=dish2, waiter=waiter,
process=print_foods, seconds=3)
manager = WoodenManager()
manager.add_customer(customer1)
manager.add_customer(customer2)
manager.launch()
while True:
time.sleep(1)
producer example:
import time
import random
from woodenwaiter.woodenwaiter import WoodenMenu
from woodenwaiter.woodenwaiter import WoodenWaiter
from woodenwaiter.woodenwaiter import WoodenCooker
if __name__ == '__main__':
table1 = 'cmdb'
table2 = 'rbac'
dish1 = 'custom_sync'
dish2 = 'some_task'
foods1 = {
"action": "sync_custom_data",
"paras": ""
}
foods2 = {
"action": "some_action",
"paras": {
"para1": "value1",
"para2": "value2"
}
}
menu1 = WoodenMenu(table=table1, dish=dish1, foods=foods1)
menu2 = WoodenMenu(table=table2, dish=dish2, foods=foods2)
waiter = WoodenWaiter()
cooker1 = WoodenCooker(menu=menu1, waiter=waiter)
cooker2 = WoodenCooker(menu=menu2, waiter=waiter)
cooker_running = True
def cook_sometime():
while cooker_running:
seconds = random.randint(3, 10)
time.sleep(seconds)
print('cookone after {} seconds'.format(seconds))
cooker1.cookone()
cooker2.cookone()
cooker_thread = threading.Thread(target=cook_sometime)
cooker_thread.start()
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
cooker_running = False
manager.terminate_all()
break
classes introduce
Interoduce Classes of WoodenWaiter
WoodenWaiter
Each WoodenWater connect ONE redis database. We use WoodenWaiter to create WoodenCooker instance and WoodenCustomer instance.
WoodenWater take dish from WoodenCooker(push task to redis list) and serve dish to WoodenCustomer(pop task from redis).
WoodenCooker
Creating an WoodenCooker need a WoodenMenu and a WoodenWaiter.
Methods:
set_menu(self, menu) - set WoodenMenu
set_waiter(self, waiter) - set WoodenWaiter
cookone(self, menu=None) - push a task to redis
WoodenCustomer
WoodenCustomer take four parameters:
table: maybe your program model
dish: maybe one kind of your task
waiter: a WoodenWaiter instance
process: a function that accept a dictionary parameter. This function will be call when waiter serve a dish of food(when pop a task from redis).
seconds: the seconds of the cycil that waiter check the redis.
WoodenCustomer is inherented from threading.Thread. But start the thread yourself is not suggested. Please use WoodenManager instand.
Methods:
call_waiter(self) - call waiter to check if foods is OK(if there is a task in redis)
call_waiter_cyclic(self, seconds) - call waiter cyclic
terminate(self) - stop customer thread genteely
WoodenManager
WoodenManager is used for WoodenCustomer centralized management. We add woodenCustomer instance to it, and then launch the threads
Methods:
add_customer(self, customer) - add WoodenCustomer instance
launch(self) - launch all WoodenCustomer Threads
terminate_all(self) - stop all customers thread genteely
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
Built Distributions
File details
Details for the file woodenwaiter-0.0.4.tar.gz
.
File metadata
- Download URL: woodenwaiter-0.0.4.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96450aebf8fa494d8414d2e17e29baf1cc7b1bd752cacbf294509ecf6c242da0 |
|
MD5 | 24bbeb0088af98dbf2231aa37d4a17c8 |
|
BLAKE2b-256 | 224171e71d8c4486cddda9c2bad555cfb7a9d13e037897635cf7e95663d0ea3c |
File details
Details for the file woodenwaiter-0.0.4-py3.5.egg
.
File metadata
- Download URL: woodenwaiter-0.0.4-py3.5.egg
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a3bc9ac0b221488e6bd63d8cb7c3bb7a400e8d4c940684ade20ac5db93f3f43 |
|
MD5 | a3e3e23ab0bb4d0ebd476dca95b3d476 |
|
BLAKE2b-256 | 462e5cb15a8c8fbe7f8e0510bf30c192bfc7da217f50dcbb7b50ea73d4c17d23 |
File details
Details for the file woodenwaiter-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: woodenwaiter-0.0.4-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 369ef5f3e84ced55f2209f99bd534dd5cd1bb6e2f4e419bd2d1aa8f672fe360a |
|
MD5 | 462132c3006a6c5bb28de77a866960d1 |
|
BLAKE2b-256 | ced9759eca05d16162eeabbbc95351926490d511adce09b3b54ff2b1d2d9dc9a |