I have implemented a producer/consumer functionality using threads in a given framework and I'm pretty sure I did not cover all possible scenarios.
I made a small reproducible version. Lines ending with # simulate the functionality of the framework.
In the original code the total number of products and consumers is not known until the end.

My goal is to wait in the producer thread until the consumer thread has finished the current product and vice versa. So the order should be:

  • consumer boot (first or 2nd step)
  • producer produce product #1 (first or 2nd step)
  • consumer consume product #1
  • producer produce product #2
  • consumer consume product #2
    until there is either no product or consumer left

This is a MRE of my code (with sleeps as placeholders), alter the value of n to simulate more or less consumers:

import threading
import time

products = [1,2,3,4,5,None]               #
prodcnt = 0                               #
n = 5                                     #

def produce():
    #simulating incremental calculation of product
    while True:                           #
        global prodcnt                    #
        product = products[prodcnt]       #
        prodcnt += 1                      #
        print("  Producer: init product "+str(product))
        if product == None:               #
            global exhausted
            exhausted = True
            print("  Producer: exhausted")
        elif noConsumers:
            print("  Producer: no consumers left")
            print("  Producer: waiting for permission to process "+str(product))
            print("  Producer: Permission granted "+str(product))
        print("  Producer: finished producing "+str(product))

exhausted = False
noConsumers = False
step = 0

getProdEvent = threading.Event()
getConsumeEvent = threading.Event()

x = threading.Thread(target=produce)    #
x.start()                               #

while not exhausted and not noConsumers:
    if step > 0:
        print("Consumer: wait for product #" +str(step)+"\n", end="")
        if not exhausted:
        print("Consumer: booting ... please wait")
    print("Consumer: finishing #"+str(step))
    if step >= n:                       # simulate no Consumers left
        print("Consumer: No consumers left")
        noConsumers = True
    step +=1 

Is there a more straight forward way to force them to wait for each other? Also I feel like my code could break easily.

The producer uses the value of step so the command step +=1 should run only if the producer is stuck in the line getConsumeEvent.wait()



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.