导包;
import Queue
import threading
直接上代码:
class ReportThreadTask(object):
task_queue = Queue.Queue()
def __init__(self):
self.immediate_thread = None
self.start()
def start(self):
self.immediate_thread = threading.Thread(target=self._exec_report_thread)
self.immediate_thread.daemon = True
self.immediate_thread.start()
def _exec_report_thread(self):
while True:
queue_len = self.task_queue.queue
if queue_len:
task = self.task_queue.get(timeout=5)
if task:
try:
# 执行线程任务
pring('1111111')
except Exception, e:
logging.error("immediate report except {0}".format(e))
time.sleep(1)
@classmethod
def add_task(cls, task):
cls.task_queue.put(task)
直接调用即可:
rtt = ReportThreadTask()
rtt.add_task(params)