本文整理匯總了Python中oslo_utils.timeutils.StopWatch方法的典型用法代碼示例。如果您正苦於以下問題:Python timeutils.StopWatch方法的具體用法?Python timeutils.StopWatch怎麽用?Python timeutils.StopWatch使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類oslo_utils.timeutils
的用法示例。
在下文中一共展示了timeutils.StopWatch方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _start
# 需要導入模塊: from oslo_utils import timeutils [as 別名]
# 或者: from oslo_utils.timeutils import StopWatch [as 別名]
def _start(self, idle_for, initial_delay=None, stop_on_exception=True):
"""Start the looping
:param idle_for: Callable that takes two positional arguments, returns
how long to idle for. The first positional argument is
the last result from the function being looped and the
second positional argument is the time it took to
calculate that result.
:param initial_delay: How long to delay before starting the looping.
Value is in seconds.
:param stop_on_exception: Whether to stop if an exception occurs.
:returns: eventlet event instance
"""
if self._thread is not None:
raise RuntimeError(self._RUN_ONLY_ONE_MESSAGE)
self.done = event.Event()
self._abort.clear()
self._thread = greenthread.spawn(
self._run_loop, idle_for,
initial_delay=initial_delay, stop_on_exception=stop_on_exception)
self._thread.link(self._on_done)
return self.done
# NOTE(bnemec): This is just a wrapper function we can mock so we aren't
# affected by other users of the StopWatch class.
示例2: wait_for_workers
# 需要導入模塊: from oslo_utils import timeutils [as 別名]
# 或者: from oslo_utils.timeutils import StopWatch [as 別名]
def wait_for_workers(self, workers=1, timeout=None):
"""Waits for geq workers to notify they are ready to do work.
NOTE(harlowja): if a timeout is provided this function will wait
until that timeout expires, if the amount of workers does not reach
the desired amount of workers before the timeout expires then this will
return how many workers are still needed, otherwise it will
return zero.
"""
if workers <= 0:
raise ValueError("Worker amount must be greater than zero")
watch = timeutils.StopWatch(duration=timeout)
watch.start()
with self._cond:
while self.total_workers < workers:
if watch.expired():
return max(0, workers - self.total_workers)
self._cond.wait(watch.leftover(return_none=True))
return 0
示例3: __init__
# 需要導入模塊: from oslo_utils import timeutils [as 別名]
# 或者: from oslo_utils.timeutils import StopWatch [as 別名]
def __init__(self, task, uuid, action,
arguments, timeout=REQUEST_TIMEOUT, result=NO_RESULT,
failures=None):
self._action = action
self._event = ACTION_TO_EVENT[action]
self._arguments = arguments
self._result = result
self._failures = failures
self._watch = timeutils.StopWatch(duration=timeout).start()
self._lock = threading.Lock()
self._machine = build_a_machine()
self._machine.initialize()
self.task = task
self.uuid = uuid
self.created_on = timeutils.now()
self.future = futurist.Future()
self.future.atom = task
示例4: wait
# 需要導入模塊: from oslo_utils import timeutils [as 別名]
# 或者: from oslo_utils.timeutils import StopWatch [as 別名]
def wait(self, timeout=None):
"""Waits until the latch is released.
:param timeout: wait until the timeout expires
:type timeout: number
:returns: true if the latch has been released before the
timeout expires otherwise false
:rtype: boolean
"""
watch = timeutils.StopWatch(duration=timeout)
watch.start()
with self._cond:
while self._count > 0:
if watch.expired():
return False
else:
self._cond.wait(watch.leftover(return_none=True))
return True
示例5: wait
# 需要導入模塊: from oslo_utils import timeutils [as 別名]
# 或者: from oslo_utils.timeutils import StopWatch [as 別名]
def wait(self, timeout=None):
# Wait until timeout expires (or forever) for jobs to appear.
watch = timeutils.StopWatch(duration=timeout)
watch.start()
with self._job_cond:
while True:
if not self._known_jobs:
if watch.expired():
raise excp.NotFound("Expired waiting for jobs to"
" arrive; waited %s seconds"
% watch.elapsed())
# This is done since the given timeout can not be provided
# to the condition variable, since we can not ensure that
# when we acquire the condition that there will actually
# be jobs (especially if we are spuriously awaken), so we
# must recalculate the amount of time we really have left.
self._job_cond.wait(watch.leftover(return_none=True))
else:
curr_jobs = self._fetch_jobs()
fetch_func = lambda ensure_fresh: curr_jobs
removal_func = lambda a_job: self._remove_job(a_job.path)
return base.JobBoardIterator(
self, LOG, board_fetch_func=fetch_func,
board_removal_func=removal_func)
示例6: _wait_for_action
# 需要導入模塊: from oslo_utils import timeutils [as 別名]
# 或者: from oslo_utils.timeutils import StopWatch [as 別名]
def _wait_for_action(self, ctx, action_id, timeout):
req = objects.ActionGetRequest(identity=action_id)
action = {}
with timeutils.StopWatch(timeout) as timeout_watch:
while not timeout_watch.expired():
action = self.rpc_client.call(ctx, 'action_get', req)
if action['status'] in [consts.ACTION_SUCCEEDED,
consts.ACTION_FAILED,
consts.ACTION_CANCELLED]:
break
eventlet.sleep(2)
if not action:
return False, "Failed to retrieve action."
elif action['status'] == consts.ACTION_SUCCEEDED:
return True, ""
elif (action['status'] == consts.ACTION_FAILED or
action['status'] == consts.ACTION_CANCELLED):
return False, "Cluster check action failed or cancelled"
return False, ("Timeout while waiting for node recovery action to "
"finish")
示例7: test_splits
# 需要導入模塊: from oslo_utils import timeutils [as 別名]
# 或者: from oslo_utils.timeutils import StopWatch [as 別名]
def test_splits(self, mock_now):
mock_now.side_effect = monotonic_iter()
watch = timeutils.StopWatch()
watch.start()
self.assertEqual(0, len(watch.splits))
watch.split()
self.assertEqual(1, len(watch.splits))
self.assertEqual(watch.splits[0].elapsed,
watch.splits[0].length)
watch.split()
splits = watch.splits
self.assertEqual(2, len(splits))
self.assertNotEqual(splits[0].elapsed, splits[1].elapsed)
self.assertEqual(splits[1].length,
splits[1].elapsed - splits[0].elapsed)
watch.stop()
self.assertEqual(2, len(watch.splits))
watch.start()
self.assertEqual(0, len(watch.splits))
示例8: _provisioning_timer
# 需要導入模塊: from oslo_utils import timeutils [as 別名]
# 或者: from oslo_utils.timeutils import StopWatch [as 別名]
def _provisioning_timer(self, timeout,
interval=_LB_STS_POLL_FAST_INTERVAL):
# REVISIT(ivc): consider integrating with Retry
max_interval = 15
with timeutils.StopWatch(duration=timeout) as timer:
while not timer.expired():
yield timer.leftover()
interval = interval * 2 * random.gauss(0.8, 0.05)
interval = min(interval, max_interval)
interval = min(interval, timer.leftover())
if interval:
time.sleep(interval)
示例9: _run_loop
# 需要導入模塊: from oslo_utils import timeutils [as 別名]
# 或者: from oslo_utils.timeutils import StopWatch [as 別名]
def _run_loop(self, idle_for_func,
initial_delay=None, stop_on_exception=True):
kind = self._KIND
func_name = reflection.get_callable_name(self.f)
func = self.f if stop_on_exception else _safe_wrapper(self.f, kind,
func_name)
if initial_delay:
self._sleep(initial_delay)
try:
watch = timeutils.StopWatch()
while self._running:
watch.restart()
result = func(*self.args, **self.kw)
watch.stop()
if not self._running:
break
idle = idle_for_func(result, self._elapsed(watch))
LOG.trace('%(kind)s %(func_name)r sleeping '
'for %(idle).02f seconds',
{'func_name': func_name, 'idle': idle,
'kind': kind})
self._sleep(idle)
except LoopingCallDone as e:
self.done.send(e.retvalue)
except Exception:
exc_info = sys.exc_info()
try:
LOG.error('%(kind)s %(func_name)r failed',
{'kind': kind, 'func_name': func_name},
exc_info=exc_info)
self.done.send_exception(*exc_info)
finally:
del exc_info
return
else:
self.done.send(True)
示例10: _delayed_process
# 需要導入模塊: from oslo_utils import timeutils [as 別名]
# 或者: from oslo_utils.timeutils import StopWatch [as 別名]
def _delayed_process(self, func):
"""Runs the function using the instances executor (eventually).
This adds a *nice* benefit on showing how long it took for the
function to finally be executed from when the message was received
to when it was finally ran (which can be a nice thing to know
to determine bottle-necks...).
"""
func_name = reflection.get_callable_name(func)
def _on_run(watch, content, message):
LOG.trace("It took %s seconds to get around to running"
" function/method '%s' with"
" message '%s'", watch.elapsed(), func_name,
ku.DelayedPretty(message))
return func(content, message)
def _on_receive(content, message):
LOG.debug("Submitting message '%s' for execution in the"
" future to '%s'", ku.DelayedPretty(message), func_name)
watch = timeutils.StopWatch()
watch.start()
try:
self._executor.submit(_on_run, watch, content, message)
except RuntimeError:
LOG.error("Unable to continue processing message '%s',"
" submission to instance executor (with later"
" execution by '%s') was unsuccessful",
ku.DelayedPretty(message), func_name,
exc_info=True)
return _on_receive
示例11: __init__
# 需要導入模塊: from oslo_utils import timeutils [as 別名]
# 或者: from oslo_utils.timeutils import StopWatch [as 別名]
def __init__(self, uuid, proxy, topics,
beat_periodicity=pr.NOTIFY_PERIOD,
worker_expiry=pr.EXPIRES_AFTER):
self._cond = threading.Condition()
self._proxy = proxy
self._topics = topics
self._workers = {}
self._uuid = uuid
self._seen_workers = 0
self._messages_processed = 0
self._messages_published = 0
self._worker_expiry = worker_expiry
self._watch = timeutils.StopWatch(duration=beat_periodicity)
示例12: wait
# 需要導入模塊: from oslo_utils import timeutils [as 別名]
# 或者: from oslo_utils.timeutils import StopWatch [as 別名]
def wait(self, timeout=None,
delay=0.01, delay_multiplier=2.0, max_delay=60.0,
sleep_func=time.sleep):
"""Wait for job to enter completion state.
If the job has not completed in the given timeout, then return false,
otherwise return true (a job failure exception may also be raised if
the job information can not be read, for whatever reason). Periodic
state checks will happen every ``delay`` seconds where ``delay`` will
be multiplied by the given multipler after a state is found that is
**not** complete.
Note that if no timeout is given this is equivalent to blocking
until the job has completed. Also note that if a jobboard backend
can optimize this method then its implementation may not use
delays (and backoffs) at all. In general though no matter what
optimizations are applied implementations must **always** respect
the given timeout value.
"""
if timeout is not None:
w = timeutils.StopWatch(duration=timeout)
w.start()
else:
w = None
delay_gen = iter_utils.generate_delays(delay, max_delay,
multiplier=delay_multiplier)
while True:
if w is not None and w.expired():
return False
if self.state == states.COMPLETE:
return True
sleepy_secs = six.next(delay_gen)
if w is not None:
sleepy_secs = min(w.leftover(), sleepy_secs)
sleep_func(sleepy_secs)
return False
示例13: wait
# 需要導入模塊: from oslo_utils import timeutils [as 別名]
# 或者: from oslo_utils.timeutils import StopWatch [as 別名]
def wait(self, timeout=None, initial_delay=0.005,
max_delay=1.0, sleep_func=time.sleep):
if initial_delay > max_delay:
raise ValueError("Initial delay %s must be less than or equal"
" to the provided max delay %s"
% (initial_delay, max_delay))
# This does a spin-loop that backs off by doubling the delay
# up to the provided max-delay. In the future we could try having
# a secondary client connected into redis pubsub and use that
# instead, but for now this is simpler.
w = timeutils.StopWatch(duration=timeout)
w.start()
delay = initial_delay
while True:
jc = self.job_count
if jc > 0:
curr_jobs = self._fetch_jobs()
if curr_jobs:
return base.JobBoardIterator(
self, LOG,
board_fetch_func=lambda ensure_fresh: curr_jobs)
if w.expired():
raise exc.NotFound("Expired waiting for jobs to"
" arrive; waited %s seconds"
% w.elapsed())
else:
remaining = w.leftover(return_none=True)
if remaining is not None:
delay = min(delay * 2, remaining, max_delay)
else:
delay = min(delay * 2, max_delay)
sleep_func(delay)
示例14: __init__
# 需要導入模塊: from oslo_utils import timeutils [as 別名]
# 或者: from oslo_utils.timeutils import StopWatch [as 別名]
def __init__(self, name, args):
self.watch = timeutils.StopWatch()
self.name = name
self.args = args
示例15: wait
# 需要導入模塊: from oslo_utils import timeutils [as 別名]
# 或者: from oslo_utils.timeutils import StopWatch [as 別名]
def wait(self, timeout=None):
with timeutils.StopWatch(timeout) as sw:
while True:
event = self._event
with _eventlet.timeout.Timeout(sw.leftover(return_none=True),
False):
event.wait()
if event is not self._event:
continue
return self.is_set()