switched from Events to queue to avoid index out of range,
and other weired multithreading bugs
This commit is contained in:
parent
8e81397cc7
commit
8222bfda15
1 changed files with 5 additions and 10 deletions
|
|
@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from logging import getLogger
|
from logging import getLogger
|
||||||
|
from Queue import Queue
|
||||||
from threading import Event, Thread
|
from threading import Event, Thread
|
||||||
from linspector.utils.singleton import Singleton
|
from linspector.utils.singleton import Singleton
|
||||||
|
|
||||||
|
|
@ -77,7 +78,7 @@ class Task(object):
|
||||||
@Singleton
|
@Singleton
|
||||||
class TaskExecutor(object):
|
class TaskExecutor(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.event = Event()
|
self.queue = Queue()
|
||||||
self.taskInfos = []
|
self.taskInfos = []
|
||||||
task_thread = Thread(target=self._run_worker_thread)
|
task_thread = Thread(target=self._run_worker_thread)
|
||||||
self._instantEnd = False
|
self._instantEnd = False
|
||||||
|
|
@ -87,16 +88,13 @@ class TaskExecutor(object):
|
||||||
|
|
||||||
def _run_worker_thread(self):
|
def _run_worker_thread(self):
|
||||||
while self.is_running() or not self.is_instant_end():
|
while self.is_running() or not self.is_instant_end():
|
||||||
if len(self.taskInfos) == 0:
|
|
||||||
self.event.clear()
|
|
||||||
self.event.wait()
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
msg, task = self.taskInfos[0]
|
msg, task = self.queue.get()
|
||||||
del self.taskInfos[0]
|
|
||||||
if task:
|
if task:
|
||||||
logger.debug("Starting Task Execution...")
|
logger.debug("Starting Task Execution...")
|
||||||
task.execute(msg)
|
task.execute(msg)
|
||||||
|
self.queue.task_done()
|
||||||
|
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
logger.error("Error " + str(e))
|
logger.error("Error " + str(e))
|
||||||
|
|
@ -109,13 +107,10 @@ class TaskExecutor(object):
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self._running = False
|
self._running = False
|
||||||
self.event.set()
|
|
||||||
|
|
||||||
def stop_immediately(self):
|
def stop_immediately(self):
|
||||||
self._running = False
|
self._running = False
|
||||||
self._instantEnd = True
|
self._instantEnd = True
|
||||||
self.event.set()
|
|
||||||
|
|
||||||
def schedule_task(self, msg, task):
|
def schedule_task(self, msg, task):
|
||||||
self.taskInfos.append((msg, task))
|
self.queue.put((msg, task))
|
||||||
self.event.set()
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue