]> git.ipfire.org Git - oddments/cappie.git/blame - cappie/queue.py
Set names of threads for better debugging.
[oddments/cappie.git] / cappie / queue.py
CommitLineData
53478050
MT
1#!/usr/bin/python
2###############################################################################
3# #
4# Cappie #
5# Copyright (C) 2010 Michael Tremer #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
22import time
23
24from threading import Thread
25
64e40ed0
MT
26import util
27
53478050
MT
28from errors import *
29
30class Queue(Thread):
31 heartbeat = 1.0
32 maxitems = 100
33
34 def __init__(self, log):
35 Thread.__init__(self)
36
37 self.log = log
38
39 self.__running = True
40 self.__queue = []
41
42 def __len__(self):
43 return self.length
44
45 def add(self, event):
46 if self.length > self.maxitems:
47 raise QueueFullError, "Cannot queue new event."
48
49 self.__queue.append(event)
50
51 @property
52 def length(self):
53 return len(self.__queue)
54
55 def run(self):
56 self.log.debug("Started event queue")
57
64e40ed0
MT
58 util.setprocname("queue")
59
53478050
MT
60 while self.__running or self.__queue:
61 if not self.__queue:
62 #self.log.debug("Queue sleeping for %s seconds" % self.heartbeat)
63 time.sleep(self.heartbeat)
64 continue
65
66 event = self.__queue.pop(0)
67 self.log.debug("Processing queue event: %s" % event)
68 try:
69 event.run()
70 except EventException, e:
71 self.log.error("Catched event exception: %s" % e)
72
73 def shutdown(self):
74 self.__running = False
75 self.log.debug("Shutting down queue")
76 self.log.debug("%d events in queue left" % len(self.__queue))
77
78 # Wait until queue handled all events
79 self.join()