Skip to main content

An easy to use implementation of the producer consumer pattern

Project description

prodcon

An easy to use implementation of the producer consumer pattern with threading

Installation

pip install prodcon

Example usage

import random
import time
from queue import Queue

from prodcon import Producer, Consumer


def generate_items(start, end):
    for i in range(start, end + 1):
        if i == end:
            # Add poison pill to stop producer and consumers
            yield None
        # Simulate producing time
        time.sleep(random.random())
        print(f'Produced item #{i}')
        yield f'Item #{i}'


def process_item(item):
    # Simulate processing time
    time.sleep(random.random() + 1)
    print(f'Processed {item}')



q = Queue()
p = Producer(q, generate_items, args=(1, 100))
c = Consumer(q, process_item)

p.start()
c.start()

Example usage with decorators

import random
import time

from prodcon import produces, consumes


@produces
def generate_items(start, end):
    for i in range(start, end + 1):
        if i == end:
            # Add poison pill to stop producer and consumers
            yield None
        # Simulate producing time
        time.sleep(random.random())
        print(f'Produced item #{i}')
        yield f'Item #{i}'


@consumes
def process_item(item):
    # Simulate processing time
    time.sleep(random.random() + 1)
    print(f'Processed {item}')


generate_items(1, 100)

# Use multiple consumers
for _ in range(5):
    process_item()

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

prodcon-0.2.0.tar.gz (2.4 kB view hashes)

Uploaded source

Built Distribution

prodcon-0.2.0-py3-none-any.whl (15.4 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page