27 lines
694 B
Python
27 lines
694 B
Python
# Copyright 2019 (c) Anna Schumaker.
|
|
import queue
|
|
import threading
|
|
|
|
class ThreadQueue(queue.Queue, threading.Thread):
|
|
def __init__(self):
|
|
queue.Queue.__init__(self)
|
|
threading.Thread.__init__(self)
|
|
self.stop_event = threading.Event()
|
|
self.start()
|
|
|
|
def push(self, func, *args):
|
|
self.put((func, args))
|
|
|
|
def run(self):
|
|
while not self.stop_event.is_set():
|
|
try:
|
|
(func, args) = self.get(block=True, timeout=0.1)
|
|
func(*args)
|
|
self.task_done()
|
|
except queue.Empty:
|
|
pass
|
|
|
|
def stop(self):
|
|
self.stop_event.set()
|
|
threading.Thread.join(self)
|