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.

Files for prodcon, version 0.2.0
Filename, size File type Python version Upload date Hashes
Filename, size prodcon-0.2.0-py3-none-any.whl (15.4 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size prodcon-0.2.0.tar.gz (2.4 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page