def drip(self):
"""Let some of the bucket drain.
How much of the bucket drains depends on how long it has been
since I was last called.
@returns: True if I am now empty.
@returntype: bool
"""
if self.parentBucket is not None:
self.parentBucket.drip()
if self.rate is None:
self.content = 0
return True
else:
now = time()
deltaT = now - self.lastDrip
self.content = long(max(0, self.content - deltaT * self.rate))
self.lastDrip = now
return False
python类time()的实例源码
def getBucketFor(self, *a, **kw):
"""You want a bucket for that? I'll give you a bucket.
Any parameters are passed on to L{getBucketKey}, from them it
decides which bucket you get.
@returntype: L{Bucket}
"""
if ((self.sweepInterval is not None)
and ((time() - self.lastSweep) > self.sweepInterval)):
self.sweep()
if self.parentFilter:
parentBucket = self.parentFilter.getBucketFor(self, *a, **kw)
else:
parentBucket = None
key = self.getBucketKey(*a, **kw)
bucket = self.buckets.get(key)
if bucket is None:
bucket = self.bucketFactory(parentBucket)
self.buckets[key] = bucket
return bucket
def _update_adabn(self, eval_data):
'''Update moving mean and moving var with eval data'''
from time import time
start = time()
with self._restore_eval_data(eval_data):
for _ in range(self.num_adabn_epoch):
eval_data.reset()
for nbatch, eval_batch in enumerate(eval_data):
self.forward(eval_batch, is_train=True)
for out in self.get_outputs():
# Cause memory leak (though not increase after this _update_adabn) without this wait
# TODO: fixme
out.wait_to_read()
# for name, block in zip(self._exec_group.aux_names, self._exec_group.aux_arrays):
# if 'moving' in name:
# for a in block:
# a.wait_to_read()
logger.debug(
'AdaBN with {} epochs takes {} seconds',
self.num_adabn_epoch,
time() - start
)
def recvall(the_socket, timeout=5):
the_socket.setblocking(0)
total_data = []
data = ""
begin = time()
while True:
sleep(0.05)
if total_data and time()-begin > timeout:
break
elif time()-begin > timeout*2:
break
try:
data = the_socket.recv(1024)
if data:
total_data.append(data)
begin = time()
except Exception:
pass
return "".join(total_data)
def ltdownload_metadata(address, binhash, metadata_queue, timeout=40):
metadata = None
start_time = time.time()
try:
session = lt.session()
r = random.randrange(10000, 50000)
session.listen_on(r, r+10)
session.add_dht_router('router.bittorrent.com',6881)
session.add_dht_router('router.utorrent.com',6881)
session.add_dht_router('dht.transmission.com',6881)
session.add_dht_router('127.0.0.1',6881)
session.start_dht()
metadata = fetch_torrent(session, binhash.encode('hex'), timeout)
session = None
except:
traceback.print_exc()
finally:
metadata_queue.put((binhash, address, metadata, 'lt', start_time))
def calcRemainingTime(self):
seekable = self.getSeek()
if seekable is not None:
len = seekable.getLength()
try:
tmp = self.cueGetEndCutPosition()
if tmp:
len = (False, tmp)
except:
pass
pos = seekable.getPlayPosition()
speednom = self.seekstate[1] or 1
speedden = self.seekstate[2] or 1
if not len[0] and not pos[0]:
if len[1] <= pos[1]:
return 0
time = (len[1] - pos[1])*speedden/(90*speednom)
return time
return False
def do_real_import(self, vsfile, filepath,mdXML,import_tags):
"""
Make the import call to vidispine, and wait for self._importer_timeout seconds for the job to complete.
Raises a VSException representing the job error if the import job fails, or ImportStalled if the timeout occurs
:param vsfile: VSFile object to import
:param filepath: filepath of the VSFile
:param mdXML: compiled metadata XML to import alongside the media
:param import_tags: shape tags describing required transcodes
:return: None
"""
import_job = vsfile.importToItem(mdXML, tags=import_tags, priority="LOW", jobMetadata={"gnm_app": "vsingester"})
job_start_time = time.time()
close_sent = False
while import_job.finished() is False:
self.logger.info("\tJob status is %s" % import_job.status())
if time.time() - job_start_time > self._importer_timeout:
self.logger.error("\tJob has taken more than {0} seconds to complete, concluding that it must be stalled.".format(self._importer_timeout))
import_job.abort()
self.logger.error("\tSent abort signal to job")
raise ImportStalled(filepath)
if time.time() - job_start_time > self._close_file_timeout and not close_sent:
vsfile.setState("CLOSED")
sleep(5)
import_job.update(noraise=False)
def drip(self):
"""Let some of the bucket drain.
How much of the bucket drains depends on how long it has been
since I was last called.
@returns: True if I am now empty.
@returntype: bool
"""
if self.parentBucket is not None:
self.parentBucket.drip()
if self.rate is None:
self.content = 0
return True
else:
now = time()
deltaT = now - self.lastDrip
self.content = long(max(0, self.content - deltaT * self.rate))
self.lastDrip = now
return False
def getBucketFor(self, *a, **kw):
"""You want a bucket for that? I'll give you a bucket.
Any parameters are passed on to L{getBucketKey}, from them it
decides which bucket you get.
@returntype: L{Bucket}
"""
if ((self.sweepInterval is not None)
and ((time() - self.lastSweep) > self.sweepInterval)):
self.sweep()
if self.parentFilter:
parentBucket = self.parentFilter.getBucketFor(self, *a, **kw)
else:
parentBucket = None
key = self.getBucketKey(*a, **kw)
bucket = self.buckets.get(key)
if bucket is None:
bucket = self.bucketFactory(parentBucket)
self.buckets[key] = bucket
return bucket
def greaterThan(self, t):
"""Compare this DateTime object to another DateTime object
OR a floating point number such as that which is returned
by the python time module.
Returns true if the object represents a date/time greater
than the specified DateTime or time module style time.
Revised to give more correct results through comparison of
long integer microseconds.
"""
if t is None:
t = 0
if isinstance(t, float):
return self._micros > long(t * 1000000)
try:
return self._micros > t._micros
except AttributeError:
return self._micros > t
def equalTo(self, t):
"""Compare this DateTime object to another DateTime object
OR a floating point number such as that which is returned
by the python time module.
Returns true if the object represents a date/time equal to
the specified DateTime or time module style time.
Revised to give more correct results through comparison of
long integer microseconds.
"""
if t is None:
t = 0
if isinstance(t, float):
return self._micros == long(t * 1000000)
try:
return self._micros == t._micros
except AttributeError:
return self._micros == t
def lessThan(self, t):
"""Compare this DateTime object to another DateTime object
OR a floating point number such as that which is returned
by the python time module.
Returns true if the object represents a date/time less than
the specified DateTime or time module style time.
Revised to give more correct results through comparison of
long integer microseconds.
"""
if t is None:
t = 0
if isinstance(t, float):
return self._micros < long(t * 1000000)
try:
return self._micros < t._micros
except AttributeError:
return self._micros < t
def lessThanEqualTo(self, t):
"""Compare this DateTime object to another DateTime object
OR a floating point number such as that which is returned
by the python time module.
Returns true if the object represents a date/time less than
or equal to the specified DateTime or time module style time.
Revised to give more correct results through comparison of
long integer microseconds.
"""
if t is None:
t = 0
if isinstance(t, float):
return self._micros <= long(t * 1000000)
try:
return self._micros <= t._micros
except AttributeError:
return self._micros <= t
def strftime(self, format):
"""Format the date/time using the *current timezone representation*."""
x = _calcDependentSecond2(self._year, self._month, self._day,
self._hour, self._minute, self._second)
ltz = self._calcTimezoneName(x, 0)
tzdiff = _tzoffset(ltz, self._t) - _tzoffset(self._tz, self._t)
zself = self + tzdiff / 86400.0
microseconds = int((zself._second - zself._nearsec) * 1000000)
unicode_format = False
if isinstance(format, explicit_unicode_type):
format = format.encode('utf-8')
unicode_format = True
ds = datetime(zself._year, zself._month, zself._day, zself._hour,
zself._minute, int(zself._nearsec),
microseconds).strftime(format)
if unicode_format:
return ds.decode('utf-8')
return ds
# General formats from previous DateTime
def ISO8601(self):
"""Return the object in ISO 8601-compatible format containing the
date, time with seconds-precision and the time zone identifier.
See: http://www.w3.org/TR/NOTE-datetime
Dates are output as: YYYY-MM-DDTHH:MM:SSTZD
T is a literal character.
TZD is Time Zone Designator, format +HH:MM or -HH:MM
If the instance is timezone naive (it was not specified with a timezone
when it was constructed) then the timezone is ommitted.
The HTML4 method below offers the same formatting, but converts
to UTC before returning the value and sets the TZD "Z".
"""
if self.timezoneNaive():
return "%0.4d-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d" % (
self._year, self._month, self._day,
self._hour, self._minute, self._second)
tzoffset = _tzoffset2iso8601zone(_tzoffset(self._tz, self._t))
return "%0.4d-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d%s" % (
self._year, self._month, self._day,
self._hour, self._minute, self._second, tzoffset)
def add_timer(self, callback, when, interval, ident):
''' Add timer to the data structure.
:param callback: Arbitrary callable object.
:type callback: ``callable object``
:param when: The first expiration time, seconds since epoch.
:type when: ``integer``
:param interval: Timer interval, if equals 0, one time timer, otherwise
the timer will be periodically executed
:type interval: ``integer``
:param ident: (optional) Timer identity.
:type ident: ``integer``
:returns: A timer object which should not be manipulated directly by
clients. Used to delete/update the timer
:rtype: ``solnlib.timer_queue.Timer``
'''
timer = Timer(callback, when, interval, ident)
self._timers.add(timer)
return timer
def get_expired_timers(self):
''' Get a list of expired timers.
:returns: a list of ``Timer``, empty list if there is no expired
timers.
:rtype: ``list``
'''
next_expired_time = 0
now = time()
expired_timers = []
for timer in self._timers:
if timer.when <= now:
expired_timers.append(timer)
if expired_timers:
del self._timers[:len(expired_timers)]
if self._timers:
next_expired_time = self._timers[0].when
return (next_expired_time, expired_timers)
def add_timer(self, callback, when, interval, ident=None):
''' Add timer to the queue.
:param callback: Arbitrary callable object.
:type callback: ``callable object``
:param when: The first expiration time, seconds since epoch.
:type when: ``integer``
:param interval: Timer interval, if equals 0, one time timer, otherwise
the timer will be periodically executed
:type interval: ``integer``
:param ident: (optional) Timer identity.
:type ident: ``integer``
:returns: A timer object which should not be manipulated directly by
clients. Used to delete/update the timer
'''
with self._lock:
timer = self._timers.add_timer(callback, when, interval, ident)
self._wakeup()
return timer
def add_timer(self, callback, when, interval, ident):
''' Add timer to the data structure.
:param callback: Arbitrary callable object.
:type callback: ``callable object``
:param when: The first expiration time, seconds since epoch.
:type when: ``integer``
:param interval: Timer interval, if equals 0, one time timer, otherwise
the timer will be periodically executed
:type interval: ``integer``
:param ident: (optional) Timer identity.
:type ident: ``integer``
:returns: A timer object which should not be manipulated directly by
clients. Used to delete/update the timer
:rtype: ``solnlib.timer_queue.Timer``
'''
timer = Timer(callback, when, interval, ident)
self._timers.add(timer)
return timer
def get_expired_timers(self):
''' Get a list of expired timers.
:returns: a list of ``Timer``, empty list if there is no expired
timers.
:rtype: ``list``
'''
next_expired_time = 0
now = time()
expired_timers = []
for timer in self._timers:
if timer.when <= now:
expired_timers.append(timer)
if expired_timers:
del self._timers[:len(expired_timers)]
if self._timers:
next_expired_time = self._timers[0].when
return (next_expired_time, expired_timers)
def add_timer(self, callback, when, interval, ident=None):
''' Add timer to the queue.
:param callback: Arbitrary callable object.
:type callback: ``callable object``
:param when: The first expiration time, seconds since epoch.
:type when: ``integer``
:param interval: Timer interval, if equals 0, one time timer, otherwise
the timer will be periodically executed
:type interval: ``integer``
:param ident: (optional) Timer identity.
:type ident: ``integer``
:returns: A timer object which should not be manipulated directly by
clients. Used to delete/update the timer
'''
with self._lock:
timer = self._timers.add_timer(callback, when, interval, ident)
self._wakeup()
return timer
def add_timer(self, callback, when, interval, ident):
''' Add timer to the data structure.
:param callback: Arbitrary callable object.
:type callback: ``callable object``
:param when: The first expiration time, seconds since epoch.
:type when: ``integer``
:param interval: Timer interval, if equals 0, one time timer, otherwise
the timer will be periodically executed
:type interval: ``integer``
:param ident: (optional) Timer identity.
:type ident: ``integer``
:returns: A timer object which should not be manipulated directly by
clients. Used to delete/update the timer
:rtype: ``solnlib.timer_queue.Timer``
'''
timer = Timer(callback, when, interval, ident)
self._timers.add(timer)
return timer
def get_expired_timers(self):
''' Get a list of expired timers.
:returns: a list of ``Timer``, empty list if there is no expired
timers.
:rtype: ``list``
'''
next_expired_time = 0
now = time()
expired_timers = []
for timer in self._timers:
if timer.when <= now:
expired_timers.append(timer)
if expired_timers:
del self._timers[:len(expired_timers)]
if self._timers:
next_expired_time = self._timers[0].when
return (next_expired_time, expired_timers)
def add_timer(self, callback, when, interval, ident=None):
''' Add timer to the queue.
:param callback: Arbitrary callable object.
:type callback: ``callable object``
:param when: The first expiration time, seconds since epoch.
:type when: ``integer``
:param interval: Timer interval, if equals 0, one time timer, otherwise
the timer will be periodically executed
:type interval: ``integer``
:param ident: (optional) Timer identity.
:type ident: ``integer``
:returns: A timer object which should not be manipulated directly by
clients. Used to delete/update the timer
'''
with self._lock:
timer = self._timers.add_timer(callback, when, interval, ident)
self._wakeup()
return timer
def drip(self):
"""
Let some of the bucket drain.
The L{Bucket} drains at the rate specified by the class
variable C{rate}.
@returns: C{True} if the bucket is empty after this drip.
@returntype: C{bool}
"""
if self.parentBucket is not None:
self.parentBucket.drip()
if self.rate is None:
self.content = 0
else:
now = time()
deltaTime = now - self.lastDrip
deltaTokens = deltaTime * self.rate
self.content = max(0, self.content - deltaTokens)
self.lastDrip = now
return self.content == 0
def getBucketFor(self, *a, **kw):
"""
Find or create a L{Bucket} corresponding to the provided parameters.
Any parameters are passed on to L{getBucketKey}, from them it
decides which bucket you get.
@returntype: L{Bucket}
"""
if ((self.sweepInterval is not None)
and ((time() - self.lastSweep) > self.sweepInterval)):
self.sweep()
if self.parentFilter:
parentBucket = self.parentFilter.getBucketFor(self, *a, **kw)
else:
parentBucket = None
key = self.getBucketKey(*a, **kw)
bucket = self.buckets.get(key)
if bucket is None:
bucket = self.bucketFactory(parentBucket)
self.buckets[key] = bucket
return bucket
def __init__(self, status, output, tick=.1, update_interval=1):
"""
:type status: Status
:type output: file
"""
super(ProgressReporterThread, self).__init__()
self.status = status
self.output = output
self._tick = tick
self._update_interval = update_interval
self._spinner_pos = 0
self._status_line = ''
self._prev_bytes = 0
self._prev_time = time()
self._should_stop = threading.Event()
def sum_up(self):
actually_downloaded = (self.status.downloaded
- self.status.resumed_from)
time_taken = self.status.time_finished - self.status.time_started
self.output.write(CLEAR_LINE)
try:
speed = actually_downloaded / time_taken
except ZeroDivisionError:
# Either time is 0 (not all systems provide `time.time`
# with a better precision than 1 second), and/or nothing
# has been downloaded.
speed = actually_downloaded
self.output.write(SUMMARY.format(
downloaded=humanize_bytes(actually_downloaded),
total=(self.status.total_size
and humanize_bytes(self.status.total_size)),
speed=humanize_bytes(speed),
time=time_taken,
))
self.output.flush()
def run(self, count=None, *args, **kwargs):
from time import time
self._start_time = time()
count = count if count else self._count
cb_count = self._callback_freq
for i, row in enumerate(self._source_pipe):
self.i = i
if count and i == count:
break
if cb_count == 0:
cb_count = self._callback_freq
self._callback(self, i)
cb_count -= 1
def calcRemainingTime(self):
seekable = self.getSeek()
if seekable is not None:
len = seekable.getLength()
try:
tmp = self.cueGetEndCutPosition()
if tmp:
len = (False, tmp)
except:
pass
pos = seekable.getPlayPosition()
speednom = self.seekstate[1] or 1
speedden = self.seekstate[2] or 1
if not len[0] and not pos[0]:
if len[1] <= pos[1]:
return 0
time = (len[1] - pos[1])*speedden/(90*speednom)
return time
return False