Skip to content

Instantly share code, notes, and snippets.

@tscheepers
Created April 5, 2021 11:42
Show Gist options
  • Save tscheepers/c97f0bbf59cf1b6b9e08d44079ed9a21 to your computer and use it in GitHub Desktop.
Save tscheepers/c97f0bbf59cf1b6b9e08d44079ed9a21 to your computer and use it in GitHub Desktop.
Producer-Consumer pattern in Python with threading
'''
Producer-Consumer pattern in Python with threading.
Be aware! This is not true parallel computing because of the GIL.
'''
import logging
import time
from threading import *
from queue import Queue
logging.basicConfig(level=logging.DEBUG)
def log(msg: str) -> None:
thread_name = current_thread().name
logging.info(f'{thread_name}: {msg}')
# Producer
def produce(queue: Queue, finished: Event, max: int) -> None:
for i in range(max):
time.sleep(0.5) # GIL releases
log(f'Producing {i}')
queue.put(i)
finished.set()
# Consumer
def consume(queue: Queue, finished: Event) -> None:
while True:
time.sleep(1) # GIL releases
if not queue.empty():
v = queue.get()
log(f'Consuming {v}')
elif finished.is_set():
log('Finished')
break
if __name__ == '__main__':
queue = Queue()
finished = Event()
log("Start")
producer = Thread(target=produce, args=[queue, finished, 30], daemon=True)
consumerA = Thread(target=consume, args=[queue, finished], daemon=True)
consumerB = Thread(target=consume, args=[queue, finished], daemon=True)
producer.start()
consumerA.start()
consumerB.start()
producer.join()
consumerA.join()
consumerB.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment