~ubuntu-branches/ubuntu/utopic/gozerbot/utopic

« back to all changes in this revision

Viewing changes to build/lib/gozerbot/threads/threadloop.py

  • Committer: Package Import Robot
  • Author(s): Jeremy Malcolm
  • Date: 2012-04-03 21:58:28 UTC
  • mfrom: (3.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20120403215828-6mik0tzug5na93la
Tags: 0.99.1-2
* Removes logfiles on purge (Closes: #668767)
* Reverted location of installed files back to /usr/lib/gozerbot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# gozerbot/threads/threadloop.py
 
2
#
 
3
#
 
4
 
 
5
""" class to implement start/stoppable threads. """
 
6
 
 
7
__copyright__ = 'this file is in the public domain'
 
8
 
 
9
# gozerbot imports
 
10
from gozerbot.utils.log import rlog
 
11
from gozerbot.config import config
 
12
from gozerbot.utils.exception import handle_exception
 
13
 
 
14
# core thread import
 
15
from thr import start_new_thread
 
16
 
 
17
# basic imports
 
18
import Queue, time
 
19
 
 
20
class ThreadLoop(object):
 
21
 
 
22
    """ implement startable/stoppable threads. """
 
23
 
 
24
    def __init__(self, name="", queue=None):
 
25
        self.name = name or 'idle'
 
26
        self.stopped = False
 
27
        self.running = False
 
28
        self.outs = []
 
29
        self.queue = queue or Queue.Queue()
 
30
        self.nowrunning = "none"
 
31
 
 
32
    def _loop(self):
 
33
        rlog(0, "threadloop", 'starting threadloop')
 
34
        while not self.stopped:
 
35
            try:
 
36
                self.running = True
 
37
                try:
 
38
                    data = self.queue.get_nowait()
 
39
                except Queue.Empty:
 
40
                    if self.stopped:
 
41
                        break
 
42
                    time.sleep(0.1)
 
43
                    continue
 
44
 
 
45
                if self.stopped:
 
46
                    break
 
47
 
 
48
                if not data:
 
49
                    break
 
50
                rlog(0, self.name, 'running %s' % str(data))
 
51
                self.handle(*data)
 
52
            except Exception, ex: handle_exception()
 
53
 
 
54
        self.running = False
 
55
        rlog(0, self.name, 'stopping threadloop')
 
56
 
 
57
    def put(self, *data):
 
58
 
 
59
        """ put data on task queue. """
 
60
 
 
61
        self.queue.put_nowait(data)
 
62
 
 
63
    def start(self):
 
64
 
 
65
        """ start the thread. """
 
66
 
 
67
        if not self.running:
 
68
            start_new_thread(self._loop, ())
 
69
 
 
70
    def stop(self):
 
71
 
 
72
        """ stop the thread. """
 
73
 
 
74
        self.stopped = True
 
75
        self.running = False
 
76
        self.queue.put(None)
 
77
 
 
78
    def handle(self, *args, **kwargs):
 
79
 
 
80
        """ overload this. """
 
81
 
 
82
        pass
 
83
 
 
84
class RunnerLoop(ThreadLoop):
 
85
 
 
86
    """ dedicated threadloop for bot commands/callbacks. """
 
87
 
 
88
 
 
89
    def _loop(self):
 
90
        rlog(0, "runnerloop", 'starting threadloop')
 
91
        self.running = True
 
92
 
 
93
        while not self.stopped:
 
94
 
 
95
            try:
 
96
                data = self.queue.get()
 
97
            except Queue.Empty:
 
98
                if self.stopped:
 
99
                    break
 
100
                time.sleep(0.1)
 
101
                continue
 
102
 
 
103
            if self.stopped:
 
104
                break
 
105
 
 
106
            if not data:
 
107
                break
 
108
 
 
109
            self.nowrunning = data[0]
 
110
            rlog(0, 'runnerloop', 'now running %s' % self.nowrunning)
 
111
            self.handle(*data)
 
112
 
 
113
        self.running = False
 
114
        rlog(0, 'runnerloop', 'stopping threadloop')
 
115
 
 
116
class ThreadSleeper(ThreadLoop):
 
117
 
 
118
    def __init__(self, name="", timeout=1800):
 
119
        ThreadLoop.__init__(self, name)
 
120
        self.timeout = timeout
 
121
 
 
122
    def _loop(self):
 
123
        rlog(0, 'threadsleeper', 'starting threadloop')
 
124
        self.running = True
 
125
 
 
126
        while not self.stopped:
 
127
            time.sleep(self.timeout)
 
128
 
 
129
            if self.stopped:
 
130
                break
 
131
            rlog(-1, self.name, 'running')
 
132
            self.handle()
 
133
 
 
134
        self.running = False
 
135
        rlog(0, 'threadsleeper', 'stopping threadloop')