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 details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
prodcon-0.2.0-py3-none-any.whl
(15.4 kB
view details)
File details
Details for the file prodcon-0.2.0.tar.gz.
File metadata
- Download URL: prodcon-0.2.0.tar.gz
- Upload date:
- Size: 2.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
74044f46448ab90a084fbb084d97cb187f0ad18fcb5cd86dd969aa0a49baa809
|
|
| MD5 |
2469b9177d2be406f7188a73200e3aed
|
|
| BLAKE2b-256 |
a29497d91844b8b328b6a316b27a302a66b9f2a8495c009a346a5a1b6e8e9f5b
|
File details
Details for the file prodcon-0.2.0-py3-none-any.whl.
File metadata
- Download URL: prodcon-0.2.0-py3-none-any.whl
- Upload date:
- Size: 15.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
83229467668b4d3d1c1602a1c093858bf17960fd43a53a56a28b0bda017507df
|
|
| MD5 |
4d2478c7c23d00d7c0e1890b04833c29
|
|
| BLAKE2b-256 |
905500ce797270c252cab49c1849cb88a57fc6fb46b1b2478e9f13c47574bf0a
|