13

I have a simple producer consumer application.

The producer is a thread that writes stuff to a queue and a consumer is a thread that reads the messages from the queue does stuff and at some points exits.

my producer looks a little bit like this

def producer(queue):
    while not queue.full():
        queue.put(randint(1, 199))

and the consumer

def consumer(queue):
    for i in range(100):
        print(queue.get())
        queue.task_done()

in my main I invoke those threads like that

p = Thread(target=producer)
c = Thread(target=consumer)

p.daemon = True
p.start()
c.start()
c.join()

when c finishes the only remaining non deamon thread is main, what is the proper way to end those threads?

update

Here is the exact code that my producer is using, since the consumer is exiting and the problem lies with the producer

def generate_random_alphanumerics(msg_queue):
    while True:
        if not msg_queue.full():
            msg_queue.put(hashlib.sha1(bytes(randint(1, 10000))).hexdigest() * 10)
        else:
            sleep(0.01)

is the problem that the thread is sleeping?

LetsPlayYahtzee
  • 7,161
  • 12
  • 41
  • 65
  • 2
    You don't have to stop daemon threads. They will be terminated automatically when the main thread ends. See [this answer](http://stackoverflow.com/questions/38804988/what-does-sys-exit-really-do-with-multiple-threads/38805873#38805873) of mine for a more detailed explanation. – martineau Dec 13 '16 at 22:37
  • but still the program hangs instead of exiting – LetsPlayYahtzee Dec 13 '16 at 22:45
  • 1
    even if I add `sys.exit()` which shouldn't be needed – LetsPlayYahtzee Dec 13 '16 at 22:47
  • 1
    @LetsPlayYahtzee: I suspect you're omitting critical parts of your code then. If you correctly set the thread as a `daemon` before `start`ing it, it should require no special contortions to exit. – ShadowRanger Dec 14 '16 at 01:19
  • LetsPlayYahtzee: No, you shouldn't need to call `sys.execit()` explicitly. The sample code in your question must not be an accurate representation of your real code. Please [edit] it after reading [_How to create a Minimal, Complete, and Verifiable example_](https://stackoverflow.com/help/mcve). – martineau Dec 14 '16 at 01:31

1 Answers1

30

The problem is that the producer loop never ends, so you should have a way to tell it to stop.

stop_event= threading.Event()
p = Thread(target=producer, args=(msg_queue, stop_event))
p.start()

And the producer becomes:

def generate_random_alphanumerics(msg_queue, stop_event):
    while not stop_event.is_set():
        if not msg_queue.full():
            [...]

Then, when you want to stop the producer just do:

stop_event.set()
enrico.bacis
  • 30,497
  • 10
  • 86
  • 115