Initial commit
This commit is contained in:
commit
351d98c523
36 changed files with 1836 additions and 0 deletions
38
pyjeeves/utils.py
Normal file
38
pyjeeves/utils.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
pyjeeves.utils
|
||||
~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Utils to improve usablity
|
||||
"""
|
||||
import threading
|
||||
|
||||
|
||||
class TaskThread(threading.Thread):
|
||||
"""Thread that executes a task every N seconds"""
|
||||
|
||||
def __init__(self):
|
||||
threading.Thread.__init__(self)
|
||||
self._finished = threading.Event()
|
||||
self._interval = 15.0
|
||||
|
||||
def setInterval(self, interval):
|
||||
"""Set the number of seconds we sleep between executing our task"""
|
||||
self._interval = interval
|
||||
|
||||
def shutdown(self):
|
||||
"""Stop this thread"""
|
||||
self._finished.set()
|
||||
|
||||
def run(self):
|
||||
while 1:
|
||||
if self._finished.isSet():
|
||||
return
|
||||
self.task()
|
||||
|
||||
# sleep for interval or until shutdown
|
||||
self._finished.wait(self._interval)
|
||||
|
||||
def task(self):
|
||||
"""The task done by this thread - override in subclasses"""
|
||||
pass
|
||||
Loading…
Add table
Add a link
Reference in a new issue