/usr/share/pyshared/jsb/lib/threadloop.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 | # jsb/threadloop.py
#
#
""" class to implement start/stoppable threads. """
## lib imports
from jsb.utils.exception import handle_exception
from threads import start_new_thread, getname
## basic imports
import Queue
import time
import logging
from collections import deque
## ThreadLoop class
class ThreadLoop(object):
""" implement startable/stoppable threads. """
def __init__(self, name="", queue=None):
self.name = name
self.stopped = False
self.running = False
self.outs = []
try: self.queue = queue or Queue.PriorityQueue()
except AttributeError: self.queue = queue or Queue.Queue()
self.nowrunning = "none"
def _loop(self):
""" the threadloops loop. """
logging.warn('starting %s' % getname(self))
self.running = True
nrempty = 0
while not self.stopped:
try: (speed, data) = self.queue.get()
except IndexError:
if self.stopped: break
time.sleep(0.1)
continue
if self.stopped: break
if not data: break
try: self.handle(*data)
except Exception, ex: handle_exception()
self.running = False
logging.warn('stopping %s' % getname(self))
def put(self, speed, *data):
""" put data on task queue. """
self.queue.put((speed, data))
def start(self):
""" start the thread. """
if not self.running and not self.stopped: return start_new_thread(self._loop, ())
def stop(self):
""" stop the thread. """
self.stopped = True
self.running = False
self.put(0, None)
def handle(self, *args, **kwargs):
""" overload this. """
pass
## RunnerLoop class
class RunnerLoop(ThreadLoop):
""" dedicated threadloop for bot commands/callbacks. """
def put(self, speed, *data):
""" put data on task queue. """
self.queue.put((speed, data))
def _loop(self):
""" runner loop. """
logging.debug('%s - starting threadloop' % self.name)
self.running = True
while not self.stopped:
try:
speed, data = self.queue.get()
if self.stopped: break
if not data: break
self.nowrunning = getname(data[1])
self.handle(speed, data)
except IndexError: time.sleep(0.1) ; continue
except Exception, ex: handle_exception()
#self.nowrunning = getname(data[1]) + "-done"
self.running = False
logging.debug('%s - stopping threadloop' % self.name)
class TimedLoop(ThreadLoop):
""" threadloop that sleeps x seconds before executing. """
def __init__(self, name, sleepsec=300, *args, **kwargs):
ThreadLoop.__init__(self, name, *args, **kwargs)
self.sleepsec = sleepsec
def _loop(self):
""" timed loop. sleep a while. """
logging.warn('%s - starting timedloop (%s seconds)' % (self.name, self.sleepsec))
self.stopped = False
self.running = True
while not self.stopped:
time.sleep(self.sleepsec)
if self.stopped: break
try: self.handle()
except Exception, ex: handle_exception()
self.running = False
logging.warn('%s - stopping timedloop' % self.name)
|