/usr/share/pyshared/jsb/lib/threads.py is in jsonbot 0.84.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | # jsb/threads.py
#
#
""" own threading wrapper. """
## jsb imports
from jsb.utils.exception import handle_exception
## basic imports
import threading
import re
import time
import thread
import logging
import uuid
## defines
# RE to determine thread name
methodre = re.compile('method\s+(\S+)', re.I)
funcre = re.compile('function\s+(\S+)', re.I)
objectre = re.compile('<(\S+)\s+object at (\S+)>', re.I)
## Botcommand class
class Botcommand(threading.Thread):
""" thread for running bot commands. """
def __init__(self, group, target, name, args, kwargs):
threading.Thread.__init__(self, None, target, name, args, kwargs)
self.name = name
self.bot = args[0]
self.ievent = args[1]
self.setDaemon(True)
def run(self):
""" run the bot command. """
try:
self.bot.benice()
result = threading.Thread.run(self)
self.ievent.ready()
except Exception, ex:
handle_exception(self.ievent)
time.sleep(1)
## Thr class
class Thr(threading.Thread):
""" thread wrapper. """
def __init__(self, group, target, name, args, kwargs):
threading.Thread.__init__(self, None, target, name, args, kwargs)
self.setDaemon(True)
self.name = name
def run(self):
""" run the thread. """
try:
logging.debug('threads - running thread %s' % self.name)
threading.Thread.run(self)
except Exception, ex:
handle_exception()
time.sleep(1)
## getname function
def getname(func):
""" get name of function/method. """
name = str(func)
method = re.search(methodre, name)
if method: name = method.group(1)
else:
function = re.search(funcre, name)
if function: name = function.group(1)
else:
object = re.search(objectre, name)
if object: name = "%s-%s" % (object.group(1), object.group(2))
else: name = str(func)
return name
## start_new_thread function
def start_new_thread(func, arglist, kwargs={}):
""" start a new thread .. set name to function/method name."""
if not kwargs: kwargs = {}
if not 'name' in kwargs:
name = getname(func)
if not name: name = str(func)
else: name = kwargs['name']
try:
thread = Thr(None, target=func, name=name, args=arglist, kwargs=kwargs)
thread.start()
return thread
except Exception, ex:
if "can't start" in str(ex): logging.error("threads - thread space is exhausted - can't start thread %s" % name)
handle_exception()
time.sleep(3)
## start_bot_cpmmand function
def start_bot_command(func, arglist, kwargs={}):
""" start a new thread .. set name to function/method name. """
if not kwargs: kwargs = {}
try:
name = getname(func)
if not name: name = 'noname'
thread = Botcommand(group=None, target=func, name=name, args=arglist, kwargs=kwargs)
thread.start()
return thread
except:
handle_exception()
time.sleep(1)
def threaded(func):
""" threading decorator. """
def threadedfunc(*args, **kwargs):
start_new_thread(func, args, kwargs)
return threadedfunc
|