How to stop daemon thread?

荒凉一梦 提交于 2019-12-03 00:32:22

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()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!