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

« back to all changes in this revision

Viewing changes to gozerbot/threads/threadloop.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2009-09-14 09:00:29 UTC
  • mfrom: (1.1.4 upstream) (3.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090914090029-uval0ekt72kmklxw
Tags: 0.9.1.3-3
Changed dependency on python-setuptools to python-pkg-resources
(Closes: #546435) 

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
 
 
12
# core thread import
 
13
from thr import start_new_thread
 
14
 
 
15
# basic imports
 
16
import Queue, time
 
17
 
 
18
class ThreadLoop(object):
 
19
 
 
20
    """ implement startable/stoppable threads. """
 
21
 
 
22
    def __init__(self, name="", queue=None):
 
23
        self.name = name or 'idle'
 
24
        self.stopped = False
 
25
        self.running = False
 
26
        self.outs = []
 
27
        self.queue = queue or Queue.Queue()
 
28
        self.nowrunning = "none"
 
29
 
 
30
    def _loop(self):
 
31
        rlog(0, self.name, 'starting threadloop')
 
32
        self.running = True
 
33
 
 
34
        while not self.stopped:
 
35
 
 
36
            try:
 
37
                data = self.queue.get()
 
38
            except Queue.Empty:
 
39
                if self.stopped:
 
40
                    break
 
41
                time.sleep(0.1)
 
42
                continue
 
43
 
 
44
            if self.stopped:
 
45
                break
 
46
 
 
47
            if not data:
 
48
                break
 
49
            rlog(-1, self.name, 'running %s' % str(data))
 
50
            self.handle(*data)
 
51
 
 
52
        self.running = False
 
53
        rlog(0, self.name, 'stopping threadloop')
 
54
 
 
55
    def put(self, *data):
 
56
 
 
57
        """ put data on task queue. """
 
58
 
 
59
        self.queue.put_nowait(data)
 
60
 
 
61
    def start(self):
 
62
 
 
63
        """ start the thread. """
 
64
 
 
65
        if not self.running:
 
66
            start_new_thread(self._loop, ())
 
67
 
 
68
    def stop(self):
 
69
 
 
70
        """ stop the thread. """
 
71
 
 
72
        self.stopped = True
 
73
        self.running = False
 
74
        self.queue.put(None)
 
75
 
 
76
    def handle(self, *args, **kwargs):
 
77
 
 
78
        """ overload this. """
 
79
 
 
80
        pass
 
81
 
 
82
class RunnerLoop(ThreadLoop):
 
83
 
 
84
    """ dedicated threadloop for bot commands/callbacks. """
 
85
 
 
86
 
 
87
    def _loop(self):
 
88
        rlog(0, self.name, 'starting threadloop')
 
89
        self.running = True
 
90
 
 
91
        while not self.stopped:
 
92
 
 
93
            try:
 
94
                data = self.queue.get()
 
95
            except Queue.Empty:
 
96
                if self.stopped:
 
97
                    break
 
98
                time.sleep(0.1)
 
99
                continue
 
100
 
 
101
            if self.stopped:
 
102
                break
 
103
 
 
104
            if not data:
 
105
                break
 
106
 
 
107
            self.nowrunning = data[0]
 
108
            rlog(0, self.name, 'now running %s' % self.nowrunning)
 
109
            self.handle(*data)
 
110
 
 
111
        self.running = False
 
112
        rlog(0, self.name, 'stopping threadloop')