本文整理汇总了Python中time._sleep函数的典型用法代码示例。如果您正苦于以下问题:Python _sleep函数的具体用法?Python _sleep怎么用?Python _sleep使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了_sleep函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: wd_test
def wd_test(log):
ws = WorkDispatcher(tmin=5, tmax=10, log=log)
i = 0
total = 0
works = []
try:
ws.serve()
while True:
wk = WorkTest(name='work_%05d' % i)
# wk.cancel()
ws.addWork(wk)
works.append(wk)
if i > 50:
ws.mgr.pauseAll()
if i > 100:
ws.mgr.resumeAll()
i += 1
total += 1
log.error('workers = %d', ws.mgr.count())
if i > 200:
break
if i < 190:
_sleep(0.3)
except Exception as e:
log.exception(e)
raise
finally:
# _sleep(1)
ws.setToStop()
ws.joinAll()
for wk in works:
log.error('[%s] status=%d', wk.name, wk.status)
log.error('total=%d, count=%d', total, WorkTest.TOTAL)
示例2: run
def run(self):
self.log.debug('[ws] start serving')
self.__wd.serve()
self.__clean.serve()
self._set_server_available()
# monitor = _Moniter(self.log)
while not self.isSetStop():
# monitor.report(self.info())
curr_task = None
start_at = _time()
try:
if not self.__task_buff.empty():
curr_task = self.__task_buff.get()
self.__curr_tasks.append(curr_task)
curr_task._call_by_ws_set_status(ST_WORKING)
self.__wd.addWorks(curr_task.subWorks)
self.log.debug('[ws] pop a Task: %s', curr_task.name)
curr_task = None
for i, tk in enumerate(self.__curr_tasks):
wkarchvied = True
for wk in tk.subWorks:
if wk.status != ST_FINISHED:
wkarchvied = False
elif wk.status == ST_ERROR:
tk._call_by_ws_set_status(ST_ERROR)
tk.setToStop()
tk.waitForStop()
self.log.debug('[ws] Task err: %s', tk.name)
del self.__curr_tasks[i]
elif wk.status == ST_CANCEL:
tk._call_by_ws_set_status(ST_CANCEL)
tk.setToStop()
tk.waitForStop()
self.log.debug('[ws] Task canceled: %s', tk.name)
del self.__curr_tasks[i]
if wkarchvied:
tk._call_by_ws_set_status(ST_FINISHED)
self.log.debug('[ws] Task done: %s', tk.name)
del self.__curr_tasks[i]
if tk.status > ST_WORKING:
self.log.debug('[ws] cleanup')
self.__clean.addWork(WorkShop.SerWork(tk))
except Exception as e:
# TODO: fetal err, need handle and report
if curr_task:
curr_task._call_by_ws_set_status(ST_ERROR)
self.log.exception(e)
finally:
duration = _time() - start_at
if duration < 0.8:
_sleep(0.5)
self._set_server_available(flag=False)
self.__wd.setToStop()
self.__wd.joinAll()
self.__cleanUp()
self.__clean.setToStop()
self.__clean.joinAll()
self.log.debug('[ws] stop serving')
示例3: put
def put(self, item, block=True, timeout=None):
"""Put item into the queue"""
if self.maxsize > 0:
if not block:
if self.qsize() >= self.maxsize:
raise Full('Redis queue is full')
elif timeout is None:
while self.qsize() >= self.maxsize:
_sleep(0.1)
elif timeout < 0:
raise ValueError("'timeout' must be a positive number")
else:
endtime = _time() + timeout
while self.qsize() >= self.maxsize:
remaining = endtime - _time()
if remaining <= 0.0:
raise Full('Redis queue is full')
_sleep(0.1)
if type(item) is not list:
item = [item, ]
elif len(item)<1:
return False
pipe = self.__db.pipeline()
for i in item:
i = self.__serialize(i)
pipe.lpush(self.key, i)
pipe.execute()
示例4: DeadlockWrap
def DeadlockWrap(function, *_args, **_kwargs):
"""DeadlockWrap(function, *_args, **_kwargs) - automatically retries
function in case of a database deadlock.
This is a function intended to be used to wrap database calls such
that they perform retrys with exponentially backing off sleeps in
between when a DBLockDeadlockError exception is raised.
A 'max_retries' parameter may optionally be passed to prevent it
from retrying forever (in which case the exception will be reraised).
d = DB(...)
d.open(...)
DeadlockWrap(d.put, "foo", data="bar") # set key "foo" to "bar"
"""
sleeptime = _deadlock_MinSleepTime
max_retries = _kwargs.get("max_retries", -1)
if _kwargs.has_key("max_retries"):
del _kwargs["max_retries"]
while 1:
try:
return function(*_args, **_kwargs)
except db.DBLockDeadlockError:
if _deadlock_VerboseFile:
_deadlock_VerboseFile.write("dbutils.DeadlockWrap: sleeping %1.3f\n" % sleeptime)
_sleep(sleeptime)
# exponential backoff in the sleep time
sleeptime *= 2
if sleeptime > _deadlock_MaxSleepTime:
sleeptime = _deadlock_MaxSleepTime
max_retries -= 1
if max_retries == -1:
raise
示例5: _blockUntilConditionMet
def _blockUntilConditionMet(
getValue,
giveUpAfterSeconds,
shouldStopEvaluator=lambda value: bool(value),
intervalBetweenSeconds=0.1,
errorMessage=None):
"""Repeatedly tries to get a value up until a time limit expires. Tries are separated by
a time interval. The call will block until shouldStopEvaluator returns True when given the value,
the default evaluator just returns the value converted to a boolean.
@return A tuple, (True, value) if evaluator condition is met, otherwise (False, None)
@raises RuntimeError if the time limit expires and an errorMessage is given.
"""
assert callable(getValue)
assert callable(shouldStopEvaluator)
assert intervalBetweenSeconds > 0.001
SLEEP_TIME = intervalBetweenSeconds * 0.5
startTime = _timer()
lastRunTime = startTime
firstRun = True # ensure we start immediately
while (_timer() - startTime) < giveUpAfterSeconds:
if firstRun or (_timer() - lastRunTime) > intervalBetweenSeconds:
firstRun = False
lastRunTime = _timer()
val = getValue()
if shouldStopEvaluator(val):
return True, val
_sleep(SLEEP_TIME)
else:
if errorMessage:
raise AssertionError(errorMessage)
return False, None
示例6: main
def main():
urls = [
# 'http://v.youku.com/v_show/id_XNzUyNDE4MTQw.html'
# 'http://i.youku.com/u/UNTc4NzI3MjY0',
# 'http://v.youku.com/v_show/id_XNzQ5NDAwMDIw.html?from=y1.1-2.10001-0.1-1',
# 'http://v.youku.com/v_show/id_XNzUwMTE2MDQw.html?f=22611771',
# 'http://v.youku.com/v_show/id_XNzQ3MjMxMTYw.html',
'http://video.sina.com.cn/p/ent/v/m/2014-08-14/102164094039.html'
]
log = util.get_logger()
bar = ProgressBar()
ws = WorkShop(tmin=1, tmax=2, log=log)
dlvs = []
for i, url in enumerate(urls):
dlvideo = VUrlTask(url, 0, 3, './tmp', bar=bar, log=log)
dlvs.append(dlvideo)
try:
ws.serve()
ws.addTasks(dlvs)
while len(dlvs) > 0:
for i, dlv in enumerate(dlvs):
if dlv.isArchived() or dlv.isError():
del dlvs[i]
_sleep(1)
except KeyboardInterrupt:
pass
except Exception as e:
log.exception(e)
finally:
ws.setToStop()
ws.join()
示例7: run
def run(self):
from random import random
counter = 0
while counter < self.quota:
counter = counter + 1
self.queue.put('%s.%d' % (self.name, counter))
_sleep(random() * 1e-05)
示例8: _check
def _check(self):
while self.running:
step = self._q.get()
self._doupdate(step=step)
self._q.task_done()
_sleep(0.1)
self._running = False
示例9: preset_recall
def preset_recall(self, preset_number, muted=False):
"""Recalls a preset on the Protea 4.24C device"""
if not (1 <= preset_number <= 30):
raise ValueError("Recalled preset must be between 1 and 31")
self.write_message(21, bytes([preset_number-1, 1]))
response = self._serial.read(10)
if not muted:
# This technique is necessary because of limitations in the 4.24C,
# in which during recall, it seems to assign values to its DSP in a
# sequential fashion, sometimes leading to temporarily insane
# values, such as high gains in both inputs and outputs, if the
# previous preset had high gain in outputs and the next preset has
# high gain in inputs.
#
# Furthermore, recalling the preset a second time allows us to
# safely mute all outputs, then to recall without muting (i.e. as
# they were saved in memory), since the input gains are already at a
# sane level by respect to the outputs.
_sleep(3.5)
self.write_message(21, bytes([preset_number-1, 0]))
response = self._serial.read(10)
示例10: main
def main(cfg, log):
if cfg.playlist:
for url in cfg.urls:
outpath, cfg.urls = parsers.getPlayListParser(url).info(url)
cfg.outpath = pjoin(cfg.outpath, outpath)
util.assure_path(cfg.outpath)
with open(pjoin(cfg.outpath, 'url.txt'), 'w') as fp:
fp.writelines([url + "\n\n"])
for i, clip in enumerate(cfg.urls):
fp.writelines(["[%03d] %s\n"%(i, clip)])
bar = ProgressBar()
ws = WorkShop(tmin=cfg.tmin, tmax=cfg.tmax, log=log)
dlvs = []
for i, url in enumerate(cfg.urls):
dlvideo = VUrlTask(url, vidfmt=cfg.vidfmt, npf=cfg.npf,
outpath=cfg.outpath, bar=bar, log=log)
dlvs.append(dlvideo)
try:
ws.serve()
ws.addTasks(dlvs)
while len(dlvs) > 0:
for i, dlv in enumerate(dlvs):
if dlv.isArchived() or dlv.isError():
del dlvs[i]
_sleep(1)
except Exception as e:
log.exception(e)
finally:
ws.setToStop()
ws.join()
示例11: _background_worker
def _background_worker(self):
try:
ni = self._isec / self._psec
itbeg = _perf_counter()
count = 0
self._rflag = True
while self._rflag:
tbeg = _perf_counter()
with self._buffers_lock:
self._manage_buffers()
for (t,i), b in self._buffers.items():
b.incr()
dat = self._ssfunc(i, t, date_time=True, throw_if_data_lost=False)
if dat:
self._parse_data(t,i,dat)
if b.count == 0:
continue
if (b.count % ni) == 0:
#print("b.count mod interval: %i" % b.count)
self._handle_null_interval(t, i, b)
count += 1
tend = _perf_counter()
trem = self._psec - (tend - tbeg)
if trem < 0:
## TODO :: this will create problems handling nulls as we wont be able
## to speed up by using WAIT_ADJ_DOWN (below)
## considering adjusting _psec
print("WARN: _background_worker taking longer than _psec (%i) seconds"
% self._psec)
_sleep( max(trem,0) * (self.WAIT_ADJ_DOWN ** self._wait_adj_down_exp) )
if (count % ni) == 0:
self._tune_background_worker(count,ni,itbeg)
finally:
self._rflag = False
示例12: _init_buffer
def _init_buffer(self, topic, item):
for _ in range(int(self._isec / self._psec)):
dat = self._ssfunc(item, topic, date_time=True, throw_if_data_lost=False)
if dat:
return dat
_sleep(self._psec)
raise TOSDB_Error("failed to get any data for first interval: %s, %s" % (topic,item))
示例13: get_return
def get_return(self, wait=1, timeout=None, raise_exception=1, alt_return=None):
"""delivers the return value or (by default) echoes the exception of
the call job
wait: 0=no waiting; Attribute error raised if no
1=waits for return value or exception
callable -> waits and wait()-call's while waiting for return
"""
if not self.done and wait:
starttime=_time()
delay=0.0005
while not self.done:
if timeout:
remaining = starttime + timeout - _time()
if remaining <= 0: #time is over
if raise_exception:
raise Empty, "return timed out"
else:
return alt_return
delay = min(delay * 2, remaining, .05)
else:
delay = min(delay * 2, .05)
if callable(wait): wait()
_sleep(delay) #reduce CPU usage by using a sleep
if self.done==2: #we had an exception
exc=self.exc
del self.exc
if raise_exception & 1: #by default exception is raised
raise exc[0],exc[1],exc[2]
else:
return alt_return
return self.ret
示例14: daemon
def daemon(cls, please_stop):
global next_ping
Till.enabled = True
try:
while not please_stop:
now = _time()
with Till.locker:
if next_ping > now:
_sleep(min(next_ping - now, INTERVAL))
continue
next_ping = now + INTERVAL
work = None
if Till.all_timers:
Till.all_timers.sort(key=lambda r: r[0])
for i, (t, s) in enumerate(Till.all_timers):
if now < t:
work, Till.all_timers[:i] = Till.all_timers[:i], []
next_ping = min(next_ping, Till.all_timers[0][0])
break
else:
work, Till.all_timers = Till.all_timers, []
if work:
for t, s in work:
s.go()
except Exception, e:
from pyLibrary.debugs.logs import Log
Log.warning("timer shutdown", cause=e)
示例15: main
def main():
my_cool_parser = argparse.ArgumentParser(description="Mock application to test Gooey's functionality")
my_cool_parser.add_argument("filename", help="Name of the file you want to read") # positional
my_cool_parser.add_argument("outfile", help="Name of the file where you'll save the output") # positional
my_cool_parser.add_argument('-c', '--countdown', default=10, type=int, help='sets the time to count down from')
my_cool_parser.add_argument("-s", "--showtime", action="store_true", help="display the countdown timer")
my_cool_parser.add_argument("-d", "--delay", action="store_true", help="Delay execution for a bit")
my_cool_parser.add_argument('--verbose', '-v', action='count')
my_cool_parser.add_argument("-o", "--obfuscate", action="store_true", help="obfuscate the countdown timer!")
my_cool_parser.add_argument('-r', '--recursive', choices=['yes', 'no'], help='Recurse into subfolders')
my_cool_parser.add_argument("-w", "--writelog", default="No, NOT whatevs", help="write log to some file or something")
my_cool_parser.add_argument("-e", "--expandAll", action="store_true", help="expand all processes")
print 'inside of main(), my_cool_parser =', my_cool_parser
args = my_cool_parser.parse_args()
print sys.argv
print args.countdown
print args.showtime
start_time = _time()
print 'Counting down from %s' % args.countdown
while _time() - start_time < args.countdown:
if args.showtime:
print 'printing message at: %s' % _time()
else:
print 'printing message at: %s' % hashlib.md5(str(_time())).hexdigest()
_sleep(.5)
print 'Finished running the program. Byeeeeesss!'