forked from linkerlin/BlackHolePy
-
Notifications
You must be signed in to change notification settings - Fork 5
/
bg_worker.py
38 lines (31 loc) · 827 Bytes
/
bg_worker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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()