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

« back to all changes in this revision

Viewing changes to gozerbot/threads/thr.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/thr.py
 
2
#
 
3
#
 
4
 
 
5
""" own threading wrapper. """
 
6
 
 
7
__copyright__ = 'this file is in the public domain'
 
8
 
 
9
# gozerbot imports
 
10
from gozerbot.stats import stats
 
11
from gozerbot.utils.exception import handle_exception
 
12
from gozerbot.utils.log import rlog
 
13
from gozerbot.utils.locking import lockdec
 
14
 
 
15
# basic imports
 
16
import threading, re, time, thread
 
17
 
 
18
dontshowthreads = ['Periodical.runjob', ]
 
19
 
 
20
# regular expression to determine thread name
 
21
methodre = re.compile('method\s+(\S+)', re.I)
 
22
funcre = re.compile('function\s+(\S+)', re.I)
 
23
 
 
24
threadlock = thread.allocate_lock()
 
25
locked = lockdec(threadlock)
 
26
 
 
27
class Botcommand(threading.Thread):
 
28
 
 
29
    """ thread for running bot commands. """
 
30
 
 
31
    def __init__(self, group, target, name, args, kwargs):
 
32
        threading.Thread.__init__(self, None, target, name, args, kwargs)
 
33
        self.name = name
 
34
        self.ievent = args[1]
 
35
        self.setDaemon(True)
 
36
 
 
37
    def run(self):
 
38
 
 
39
        """ run the bot command. """
 
40
 
 
41
        try:
 
42
            rlog(10, 'thr', 'running bot command thread %s' % self.name) 
 
43
            stats.up('threads', self.name)
 
44
            result = threading.Thread.run(self)
 
45
 
 
46
            if self.ievent.closequeue:
 
47
                rlog(4, 'thr', 'closing queue for %s' % self.ievent.userhost)
 
48
                for i in self.ievent.queues:
 
49
                    i.put_nowait(None)
 
50
 
 
51
        except Exception, ex:
 
52
            handle_exception(self.ievent)
 
53
            time.sleep(1)
 
54
 
 
55
class Thr(threading.Thread):
 
56
 
 
57
    """ thread wrapper. """
 
58
 
 
59
    def __init__(self, group, target, name, args, kwargs):
 
60
        threading.Thread.__init__(self, None, target, name, args, kwargs)
 
61
        rlog(-14, 'thr', 'running %s .. args: %s' % (name, args))
 
62
        self.setDaemon(True)
 
63
        self.name = name
 
64
        #time.sleep(0.001)
 
65
 
 
66
    def run(self):
 
67
        """ run the thread. """
 
68
        try:
 
69
            if self.name not in dontshowthreads:
 
70
                rlog(-4, 'thr', 'running thread %s' % self.name) 
 
71
            stats.up('threads', self.name)
 
72
            threading.Thread.run(self)
 
73
        except Exception, ex:
 
74
            handle_exception()
 
75
            time.sleep(1)
 
76
 
 
77
def getname(func):
 
78
 
 
79
    """ get name of function/method. """
 
80
 
 
81
    name = ""
 
82
 
 
83
    method = re.search(methodre, str(func))
 
84
    if method:
 
85
        name = method.group(1)
 
86
    else: 
 
87
        function = re.search(funcre, str(func))
 
88
        if function:
 
89
            name = function.group(1)
 
90
        else:
 
91
            name = str(func)
 
92
    return name
 
93
 
 
94
#@locked
 
95
def start_new_thread(func, arglist, kwargs=None):
 
96
 
 
97
    """ start a new thread .. set name to function/method name."""
 
98
 
 
99
    if not kwargs:
 
100
        kwargs = {}
 
101
 
 
102
    try:
 
103
        name = getname(func)
 
104
        if not name:
 
105
            name = 'noname'
 
106
        thread = Thr(None, target=func, name=name, args=arglist, \
 
107
kwargs=kwargs)
 
108
        rlog(-1, 'thr', 'starting %s thread' % str(func))
 
109
        thread.start()
 
110
        return thread
 
111
    except:
 
112
        handle_exception()
 
113
        time.sleep(1)
 
114
 
 
115
#@locked
 
116
def start_bot_command(func, arglist, kwargs={}):
 
117
 
 
118
    """ start a new thread .. set name to function/method name. """
 
119
 
 
120
    if not kwargs:
 
121
        kwargs = {}
 
122
 
 
123
    try:
 
124
        name = getname(func)
 
125
        if not name:
 
126
            name = 'noname'
 
127
        thread = Botcommand(group=None, target=func, name=name, args=arglist, \
 
128
kwargs=kwargs)
 
129
        rlog(-1, 'thr', 'starting %s thread' % str(func))
 
130
        thread.start()
 
131
        return thread
 
132
    except:
 
133
        handle_exception()
 
134
        time.sleep(1)
 
135
 
 
136
def threaded(func):
 
137
 
 
138
    """ threading decorator. """
 
139
 
 
140
    def threadedfunc(*args, **kwargs):
 
141
        start_new_thread(func, args, kwargs)
 
142
    return threadedfunc