This repository has been archived by the owner on Oct 12, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
BackgroundTaskQueue
81 lines (61 loc) · 2.06 KB
/
BackgroundTaskQueue
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
This engine plugin allows you to run tasks in response to a request without tying up the request thread.
{{{
#!python
import Queue
import threading
class BackgroundTaskQueue(SimplePlugin):
thread = None
def __init__(self, bus, qsize=100, qwait=2, safe_stop=True):
SimplePlugin.__init__(self, bus)
self.q = Queue.Queue(qsize)
self.qwait = qwait
self.safe_stop = safe_stop
def start(self):
self.running = True
if not self.thread:
self.thread = threading.Thread(target=self.run)
self.thread.start()
def stop(self):
if self.safe_stop:
self.running = "draining"
else:
self.running = False
if self.thread:
self.thread.join()
self.thread = None
self.running = False
def run(self):
while self.running:
try:
try:
func, args, kwargs = self.q.get(block=True, timeout=self.qwait)
except Queue.Empty:
if self.running == "draining":
return
continue
else:
func(*args, **kwargs)
if hasattr(self.q, 'task_done'):
self.q.task_done()
except:
self.bus.log("Error in BackgroundTaskQueue %r." % self,
level=40, traceback=True)
def put(self, func, *args, **kwargs):
"""Schedule the given func to be run."""
self.q.put((func, args, kwargs))
}}}
Use it like this:
{{{
#!python
import cherrypy
from complicated_logging import log
bgtask = BackgroundTaskQueue(cherrypy.engine)
bgtask.subscribe()
class Root(object):
def index(self):
bgtask.put(log, "index was called", ip=cherrypy.request.remote.ip))
return "Hello, world!"
index.exposed = True
}}}
----
Another interesting feature for tasks: [IsClientConnected]