5
""" own threading wrapper. """
7
__copyright__ = 'this file is in the public domain'
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
16
import threading, re, time, thread
18
dontshowthreads = ['Periodical.runjob', ]
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)
24
threadlock = thread.allocate_lock()
25
locked = lockdec(threadlock)
27
class Botcommand(threading.Thread):
29
""" thread for running bot commands. """
31
def __init__(self, group, target, name, args, kwargs):
32
threading.Thread.__init__(self, None, target, name, args, kwargs)
39
""" run the bot command. """
42
rlog(10, 'thr', 'running bot command thread %s' % self.name)
43
stats.up('threads', self.name)
44
result = threading.Thread.run(self)
46
if self.ievent.closequeue:
47
rlog(4, 'thr', 'closing queue for %s' % self.ievent.userhost)
48
for i in self.ievent.queues:
52
handle_exception(self.ievent)
55
class Thr(threading.Thread):
57
""" thread wrapper. """
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))
67
""" run the thread. """
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)
79
""" get name of function/method. """
83
method = re.search(methodre, str(func))
85
name = method.group(1)
87
function = re.search(funcre, str(func))
89
name = function.group(1)
95
def start_new_thread(func, arglist, kwargs=None):
97
""" start a new thread .. set name to function/method name."""
106
thread = Thr(None, target=func, name=name, args=arglist, \
108
rlog(-1, 'thr', 'starting %s thread' % str(func))
116
def start_bot_command(func, arglist, kwargs={}):
118
""" start a new thread .. set name to function/method name. """
127
thread = Botcommand(group=None, target=func, name=name, args=arglist, \
129
rlog(-1, 'thr', 'starting %s thread' % str(func))
138
""" threading decorator. """
140
def threadedfunc(*args, **kwargs):
141
start_new_thread(func, args, kwargs)