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
Post a Comment