Package gozerbot :: Module thr
[hide private]
[frames] | no frames]

Source Code for Module gozerbot.thr

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