Merge "Add a timeout object that can be interrupted"
This commit is contained in:
@@ -42,7 +42,7 @@ class WorkerTaskExecutor(executor.TaskExecutorBase):
|
|||||||
self._on_wait, **kwargs)
|
self._on_wait, **kwargs)
|
||||||
self._proxy_thread = None
|
self._proxy_thread = None
|
||||||
self._notify_thread = None
|
self._notify_thread = None
|
||||||
self._notify_event = threading.Event()
|
self._notify_timeout = misc.Timeout(pr.NOTIFY_PERIOD)
|
||||||
|
|
||||||
def _make_thread(self, target):
|
def _make_thread(self, target):
|
||||||
thread = threading.Thread(target=target)
|
thread = threading.Thread(target=target)
|
||||||
@@ -167,9 +167,9 @@ class WorkerTaskExecutor(executor.TaskExecutorBase):
|
|||||||
def _notify_topics(self):
|
def _notify_topics(self):
|
||||||
"""Cyclically publish notify message to each topic."""
|
"""Cyclically publish notify message to each topic."""
|
||||||
LOG.debug("Notify thread started.")
|
LOG.debug("Notify thread started.")
|
||||||
while not self._notify_event.is_set():
|
while not self._notify_timeout.is_stopped():
|
||||||
self._proxy.publish(pr.Notify(), self._topics, reply_to=self._uuid)
|
self._proxy.publish(pr.Notify(), self._topics, reply_to=self._uuid)
|
||||||
self._notify_event.wait(pr.NOTIFY_PERIOD)
|
self._notify_timeout.wait()
|
||||||
|
|
||||||
def execute_task(self, task, task_uuid, arguments,
|
def execute_task(self, task, task_uuid, arguments,
|
||||||
progress_callback=None):
|
progress_callback=None):
|
||||||
@@ -199,7 +199,7 @@ class WorkerTaskExecutor(executor.TaskExecutorBase):
|
|||||||
"""Stop proxy, so its thread would be gracefully terminated."""
|
"""Stop proxy, so its thread would be gracefully terminated."""
|
||||||
if self._proxy_thread is not None:
|
if self._proxy_thread is not None:
|
||||||
if self._proxy_thread.is_alive():
|
if self._proxy_thread.is_alive():
|
||||||
self._notify_event.set()
|
self._notify_timeout.interrupt()
|
||||||
self._notify_thread.join()
|
self._notify_thread.join()
|
||||||
self._proxy.stop()
|
self._proxy.stop()
|
||||||
self._proxy_thread.join()
|
self._proxy_thread.join()
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import logging
|
|||||||
import os
|
import os
|
||||||
import string
|
import string
|
||||||
import sys
|
import sys
|
||||||
|
import threading
|
||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
@@ -210,6 +211,31 @@ class AttrDict(dict):
|
|||||||
self[name] = value
|
self[name] = value
|
||||||
|
|
||||||
|
|
||||||
|
class Timeout(object):
|
||||||
|
"""An object which represents a timeout.
|
||||||
|
|
||||||
|
This object has the ability to be interrupted before the actual timeout
|
||||||
|
is reached.
|
||||||
|
"""
|
||||||
|
def __init__(self, timeout):
|
||||||
|
if timeout < 0:
|
||||||
|
raise ValueError("Timeout must be >= 0 and not %s" % (timeout))
|
||||||
|
self._timeout = timeout
|
||||||
|
self._event = threading.Event()
|
||||||
|
|
||||||
|
def interrupt(self):
|
||||||
|
self._event.set()
|
||||||
|
|
||||||
|
def is_stopped(self):
|
||||||
|
return self._event.is_set()
|
||||||
|
|
||||||
|
def wait(self):
|
||||||
|
self._event.wait(self._timeout)
|
||||||
|
|
||||||
|
def reset(self):
|
||||||
|
self._event.clear()
|
||||||
|
|
||||||
|
|
||||||
class ExponentialBackoff(object):
|
class ExponentialBackoff(object):
|
||||||
"""An iterable object that will yield back an exponential delay sequence
|
"""An iterable object that will yield back an exponential delay sequence
|
||||||
provided an exponent and a number of items to yield. This object may be
|
provided an exponent and a number of items to yield. This object may be
|
||||||
|
|||||||
Reference in New Issue
Block a user