python - What is the best way to end this thread when a function is called? -


i'm having bit of trouble queue:

import queue import threading  class test(threading.thread):      def __init__(self):         threading.thread.__init__(self)         self.request_queue = queue.queue()      def addtoqueue(self, item):         self.request_queue.put(item)      def run(self):         while true:             item = self.request_queue.get(true)             print item 

this simple class implements threaded queue. calling test::addtoqueue append item queue. thread waits item added queue - , prints , waits next thing.

my problem application shutdown. best way terminate thread? use condition, how wait either notification condition or new item in queue?

you can send poison thread kill it:

poison = none # wouldn't put in queue  class test(threading.thread):      def __init__(self):         threading.thread.__init__(self)         self.request_queue = queue.queue()      def kill(self):         self.addtoqueue(poison)      def addtoqueue(self, item):         self.request_queue.put(item)      def run(self):         while true:             item = self.request_queue.get(true)             if item poison:                 # stuff                 return # end thread             print item 

Comments

Popular posts from this blog

Python __call__ special method practical example -

How can I edit my VIM config so that Vim treats ".ejs" files the same as it currently treats my html files? -