From 6facca4cb39d2fe2653b0078b95b470e6a961721 Mon Sep 17 00:00:00 2001 From: "miao.lin" Date: Fri, 28 Jun 2013 15:25:31 +0800 Subject: [PATCH] Added a class for background working --- src/class_bgWorker.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/class_bgWorker.py diff --git a/src/class_bgWorker.py b/src/class_bgWorker.py new file mode 100644 index 00000000..9e374ee6 --- /dev/null +++ b/src/class_bgWorker.py @@ -0,0 +1,38 @@ +#! /usr/bin/python +# -*- coding: utf-8 -*- +# cody by linker.lin@me.com + +__author__ = 'linkerlin' + + +import threading +import Queue +import time + +class BGWorker(threading.Thread): + def __init__(self): + threading.Thread.__init__(self) + self.q = Queue.Queue() + + def post(self,job): + self.q.put(job) + + def run(self): + while 1: + job=None + try: + job = self.q.get(block=True) + if job: + job() + except Exception as ex: + print "Error,job exception:",ex.message,type(ex) + time.sleep(0.05) + else: + #print "job: ", job, " done" + pass + finally: + time.sleep(0.05) + +bgworker = BGWorker() +bgworker.setDaemon(True) +bgworker.start()