Skip to main content

A producer-customer model based on redis

Project description

Python producer-customer model based on redis

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)

    def cook_sometime():
        while True:
            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:
        time.sleep(1)

classes introduce

Interoduce Classes of WoodenWaiter

WoodenMenu

Each WoodenCooker cook ONE kind of food according to ONE WoodenMenu instance. We create WoodenMenu instance by offer a table(maybe your program model), a dish(maybe one kinds of your task) and foods(some informations the customer need)

suggested format of parameter ‘foods’:

foods = {
    "action": "要执行的任务字符串",
    "paras": {"para1": "value1", "para2": "value2"}
}

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

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

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

woodenwaiter-0.0.3.tar.gz (4.4 kB view details)

Uploaded Source

Built Distributions

woodenwaiter-0.0.3-py3.5.egg (8.8 kB view details)

Uploaded Source

woodenwaiter-0.0.3-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file woodenwaiter-0.0.3.tar.gz.

File metadata

File hashes

Hashes for woodenwaiter-0.0.3.tar.gz
Algorithm Hash digest
SHA256 f75bb606fb337b7d77302dc3420123e452e8fa46b77185a9b4f67f1ff613c0ae
MD5 ddd9bab67d9f1f505ff9cb656a7587fc
BLAKE2b-256 fb8c511f30710db38042304ff4eda428b297d5f9a56090fc560d901e784053dd

See more details on using hashes here.

File details

Details for the file woodenwaiter-0.0.3-py3.5.egg.

File metadata

File hashes

Hashes for woodenwaiter-0.0.3-py3.5.egg
Algorithm Hash digest
SHA256 e904d73e3d25e2df1bf9b7cc74b100f93786d5cf138831c3d947ee812a26aec8
MD5 7bceda56c2e40a7a7c148af1c0fed6f9
BLAKE2b-256 a4fb735384d42b24c29d9bfbcde7542a7ecc5dd1e0bdc3fff7d7c4e69f71435f

See more details on using hashes here.

File details

Details for the file woodenwaiter-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for woodenwaiter-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 950d44e1de63062b142c484f82c1de1e4028ebedf43e768104ce6df5084baa2e
MD5 a3c15a6f713419c11350cf6d1844785d
BLAKE2b-256 5ff61f36c04773a4b19ec672b679654ef7f66ce68e63963668b2dfbb0bc429ac

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page