本文整理匯總了Python中time.ticks_diff方法的典型用法代碼示例。如果您正苦於以下問題:Python time.ticks_diff方法的具體用法?Python time.ticks_diff怎麽用?Python time.ticks_diff使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類time
的用法示例。
在下文中一共展示了time.ticks_diff方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: publish
# 需要導入模塊: import time [as 別名]
# 或者: from time import ticks_diff [as 別名]
def publish(self, topic, msg, retain=False, qos=0, timeout=None):
task = None
start = time.ticks_ms()
while timeout is None or time.ticks_diff(time.ticks_ms(), start) < timeout:
# Can't use wait_for because cancelling a wait_for would cancel _publishTimeout
# Also a timeout in wait_for would cancel _publishTimeout without waiting for
# the socket lock to be available, breaking mqtt protocol.
if self._pub_task is None and task is None:
task = asyncio.create_task(self._publishTimeout(topic, msg, retain, qos))
self._pub_task = task
elif task is not None:
if self._pub_task != task:
return # published
await asyncio.sleep_ms(20)
if task is not None:
async with self.lock:
task.cancel()
return
示例2: check
# 需要導入模塊: import time [as 別名]
# 或者: from time import ticks_diff [as 別名]
def check(self):
current_time = time.ticks_ms()
deadline = time.ticks_add(self.last_measurement, self.interval)
if ((time.ticks_diff(deadline, current_time) <= 0) or (self.iterations == 0)):
self.measure()
self.iterations += 1
self.last_measurement = current_time
# the following function, when added to the google sheet (Tools > Script editor) allows the
# formula uploaded in the "now" variable (see "measure(self)") to calculate a local timestamp
# from the epoch value loaded in column A of the inserted row
#
#function TIMESTAMP_TO_DATE(value) {
# return new Date(value * 1000);
#}
# see the sheets.py file to set the ValueInputOption to USER_INPUT to avoid now string value being prefixed with a '
示例3: push_sorted
# 需要導入模塊: import time [as 別名]
# 或者: from time import ticks_diff [as 別名]
def push_sorted(self, v, data):
v.data = data
if ticks_diff(data, ticks()) <= 0:
cur = self.last
if cur and ticks_diff(data, cur.data) >= 0:
# Optimisation: can start looking from self.last to insert this item
while cur.next and ticks_diff(data, cur.next.data) >= 0:
cur = cur.next
v.next = cur.next
cur.next = v
self.last = cur
return
cur = self
while cur.next and (not isinstance(cur.next.data, int) or ticks_diff(data, cur.next.data) >= 0):
cur = cur.next
v.next = cur.next
cur.next = v
if cur is not self:
self.last = cur
示例4: _loop
# 需要導入模塊: import time [as 別名]
# 或者: from time import ticks_diff [as 別名]
def _loop(self):
mqtt = config.getMQTT()
mqtt.registerWifiCallback(self._wifiChanged)
mqtt.registerConnectedCallback(self._reconnected)
await self._flash(500, 1)
sta = network.WLAN(network.STA_IF)
st = time.ticks_ms()
while True:
while self._next:
await self._flash(*self._next.pop(0))
await asyncio.sleep(1)
if time.ticks_diff(time.ticks_ms(), st) > 60000: # heartbeat
st = time.ticks_ms()
if sta.isconnected():
await self._flash(20, 1)
await asyncio.sleep_ms(250)
await self._flash(20, 1)
else:
await self._flash(500, 3)
await asyncio.sleep_ms(500)
示例5: __bell
# 需要導入模塊: import time [as 別名]
# 或者: from time import ticks_diff [as 別名]
def __bell(self):
while True:
await self._event_bell
diff = time.ticks_diff(time.ticks_ms(), self._last_activation)
if diff > 10000:
_log.error("Bell rang {!s}s ago, not activated ringing".format(diff / 1000))
self._event_bell.clear()
return
else:
on = await _mqtt.publish(self.topic(), "ON", qos=1, timeout=2,
await_connection=False)
await asyncio.sleep_ms(self._on_time)
await _mqtt.publish(self.topic(), "OFF", qos=1, retain=True, await_connection=on)
if config.RTC_SYNC_ACTIVE:
t = time.localtime()
await _mqtt.publish(_mqtt.getDeviceTopic("last_bell"),
"{}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}".format(t[0], t[1],
t[2], t[3],
t[4], t[5]),
qos=1, retain=True, timeout=2, await_connection=False)
self._event_bell.clear()
if diff > 500:
_log.warn("Bell rang {!s}ms ago, activated ringing".format(diff))
示例6: overwatch
# 需要導入模塊: import time [as 別名]
# 或者: from time import ticks_diff [as 別名]
def overwatch(coro_name, threshold, asyncr=False):
def func_wrapper(coro):
if asyncr is True:
raise TypeError("overwatch does not support coroutines")
# as it makes not sense. a freeze would trigger every coroutine
else:
def wrapper(*args, **kwargs):
startt = time.ticks_ms()
res = coro(*args, **kwargs)
if str(type(res)) == "<class 'generator'>":
_log.error("Coroutine in sync overwatch")
endt = time.ticks_ms()
diff = time.ticks_diff(endt, startt)
if diff > threshold:
_log.error("Coro {!s} took {!s}ms, threshold {!s}ms".format(coro_name, diff, threshold))
return res
return wrapper
return func_wrapper
示例7: timeit
# 需要導入模塊: import time [as 別名]
# 或者: from time import ticks_diff [as 別名]
def timeit():
spi = SpiMaster(1, baudrate=int(pyb.freq()[3] / 16))
start = ticks_ms()
for i in range(2 ** 10):
spi.write_data(b'abcdefgh' * 4)
spi.read_data()
print("Millisecond ticks elapsed: %i" % ticks_diff(ticks_ms(), start))
示例8: _read_timeout
# 需要導入模塊: import time [as 別名]
# 或者: from time import ticks_diff [as 別名]
def _read_timeout(cnt, timeout_ms=2000):
time_support = "ticks_ms" in dir(time)
s_time = time.ticks_ms() if time_support else 0
data = sys.stdin.read(cnt)
if len(data) != cnt or (time_support and time.ticks_diff(time.ticks_ms(), s_time) > timeout_ms):
return None
return data
示例9: ticks_diff
# 需要導入模塊: import time [as 別名]
# 或者: from time import ticks_diff [as 別名]
def ticks_diff(new, old):
if USE_UTIME:
return time.ticks_diff(new, old)
else:
return new - old
示例10: __init__
# 需要導入模塊: import time [as 別名]
# 或者: from time import ticks_diff [as 別名]
def __init__(self, timediff):
if timediff is None:
self.expect_ts = False
if is_micropython:
self.timediff = lambda start, end : time.ticks_diff(start, end)/1000000
else:
raise ValueError('You must define a timediff function')
else:
self.expect_ts = True
self.timediff = timediff
self.start_time = None
示例11: TimeDiff
# 需要導入模塊: import time [as 別名]
# 或者: from time import ticks_diff [as 別名]
def TimeDiff(start, end):
return time.ticks_diff(start, end)/1000000
示例12: ppm
# 需要導入模塊: import time [as 別名]
# 或者: from time import ticks_diff [as 別名]
def ppm(self, publish=True, timeout=5):
if time.ticks_diff(time.ticks_ms(), self._time) > 5000:
await self._read(publish, timeout)
return self._ppm
示例13: _subscribeTopics
# 需要導入模塊: import time [as 別名]
# 或者: from time import ticks_diff [as 別名]
def _subscribeTopics(self, start: int = 0):
_log.debug("_subscribeTopics, start", start, local_only=True)
try:
for i, sub in enumerate(self._subs):
# do not iter by range(start,length(_subs)) as _subs could get bigger while itering
if i < start:
continue # iter until start position reached
t = sub[0]
if self.isDeviceTopic(t):
t = self.getRealTopic(t)
if len(sub) == 4: # requested retained state topic
self._sub_retained = True
# if coro gets canceled in the process, the state topic will be checked
# the next time _subscribeTopic runs after the reconnect
_log.debug("_subscribing", t[:-4], local_only=True)
await self._preprocessor(super().subscribe, t[:-4], 1) # subscribe state topic
ts = time.ticks_ms() # start timer after successful subscribe otherwise
# it might time out before subscribe has even finished.
while time.ticks_diff(time.ticks_ms(), ts) < 4000 and self._sub_retained:
# wait 4 seconds for answer
await asyncio.sleep_ms(100)
if self._sub_retained is True: # no state message received
self._subs[i] = sub[:3]
self._sub_retained = False
_log.debug("Unsubscribing state topic", t[:-4], "in _subsscribeTopics",
local_only=True)
await self._preprocessor(super().unsubscribe, t[:-4],
await_connection=False)
_log.debug("_subscribing", t, local_only=True)
await self._preprocessor(super().subscribe, t, 1)
# no timeouts because _subscribeTopics will get canceled when connection is lost
finally:
# remove pending unsubscribe requests
for sub in self.__unsub_tmp:
self._subs.remove(sub)
self.__unsub_tmp = []
self._sub_coro = None
示例14: awaitSubscriptionsDone
# 需要導入模塊: import time [as 別名]
# 或者: from time import ticks_diff [as 別名]
def awaitSubscriptionsDone(self, timeout=None, await_connection=True):
start = time.ticks_ms()
while timeout is None or time.ticks_diff(time.ticks_ms(), start) < timeout * 1000:
if not await_connection and not self._isconnected:
return False
if self._sub_coro is None:
return True # all topics subscribed.
await asyncio.sleep_ms(50)
return False # timeout
示例15: _preprocessor
# 需要導入模塊: import time [as 別名]
# 或者: from time import ticks_diff [as 別名]
def _preprocessor(self, coroutine, *args, timeout=None, await_connection=True):
coro = None
start = time.ticks_ms()
i = 0 if len(args) == 4 else 1 # 0: publish, 1:(un)sub
try:
while timeout is None or time.ticks_diff(time.ticks_ms(), start) < timeout * 1000:
if not await_connection and not self._isconnected:
return False
if self._ops_coros[i] is coro is None:
coro = self._operationTimeout(coroutine, *args, i=i)
asyncio.get_event_loop().create_task(coro)
self._ops_coros[i] = coro
elif coro:
if self._ops_coros[i] != coro:
return True # published
await asyncio.sleep_ms(20)
_log.debug("timeout on", "(un)sub" if i else "publish", args, local_only=True)
self.__timedout += 1
except asyncio.CancelledError:
raise # the caller should be cancelled too
finally:
if coro and self._ops_coros[i] == coro:
async with self.lock:
asyncio.cancel(coro)
return False
# else: returns value during process
return False