本文整理汇总了Python中uasyncio.sleep方法的典型用法代码示例。如果您正苦于以下问题:Python uasyncio.sleep方法的具体用法?Python uasyncio.sleep怎么用?Python uasyncio.sleep使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类uasyncio
的用法示例。
在下文中一共展示了uasyncio.sleep方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sleep
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep [as 别名]
def sleep(t, granularity=100): # 100ms default
if granularity <= 0:
raise ValueError('sleep granularity must be > 0')
t = int(t * 1000) # ms
if t <= granularity:
await asyncio.sleep_ms(t)
else:
n, rem = divmod(t, granularity)
for _ in range(n):
await asyncio.sleep_ms(granularity)
await asyncio.sleep_ms(rem)
# Anonymous cancellable tasks. These are members of a group which is identified
# by a user supplied name/number (default 0). Class method cancel_all() cancels
# all tasks in a group and awaits confirmation. Confirmation of ending (whether
# normally or by cancellation) is signalled by a task calling the _stopped()
# class method. Handled by the @cancellable decorator.
示例2: main
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep [as 别名]
def main(client):
try:
await client.connect()
except OSError:
print('Connection failed.')
return
n = 0
s = '{} repubs: {} outages: {} rssi: {}dB free: {}bytes'
while True:
await asyncio.sleep(5)
gc.collect()
m = gc.mem_free()
print('publish', n)
# If WiFi is down the following will pause for the duration.
await client.publish(TOPIC, s.format(n, client.REPUB_COUNT, outages, rssi, m), qos = 1)
n += 1
# Define configuration
示例3: wan_ok
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep [as 别名]
def wan_ok(self,
packet=b'$\x1a\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01'):
if not self.isconnected(): # WiFi is down
return False
length = 32 # DNS query and response packet size
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setblocking(False)
s.connect(('8.8.8.8', 53))
await asyncio.sleep(1)
try:
await self._as_write(packet, sock=s)
await asyncio.sleep(2)
res = await self._as_read(length, s)
if len(res) == length:
return True # DNS response size OK
except OSError: # Timeout on read: no connectivity.
return False
finally:
s.close()
return False
示例4: heartbeat
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep [as 别名]
def heartbeat():
led = pyb.LED(1)
while True:
await asyncio.sleep_ms(500)
led.toggle()
# Replace to handle status changes. In the case of fatal status values the
# ESP8266 will be rebooted on return. You may want to pause for remedial
# action before the reboot. Information statuses can be ignored with rapid
# return. Cases which may require attention:
# SPECNET return 1 to try specified network or 0 to reboot ESP. Code below
# tries specified LAN (if default LAN fails) on first run only to limit
# flash wear.
# BROKER_FAIL Pause for server fix? Return (and so reboot) after delay?
# Pauses must be implemented with the following to ensure task quits on fail
# if not await self.exit_gate.sleep(delay_in_secs):
# return
示例5: _do_rtc
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep [as 别名]
def _do_rtc(self):
lnk = self._lnk
lnk.vbprint('Start RTC synchroniser')
self._time_valid = not self._rtc_last_syn == 0 # Valid on restart
while True:
while not self._time_valid:
lnk.channel.send(TIME)
# Give 5s time for response
await asyn.sleep(5)
if not self._time_valid:
# WiFi may be down. Delay 1 min before retry.
await asyn.sleep(60)
else: # Valid time received or restart
if self._rtc_interval < 0:
break # One resync only: done
tend = self._rtc_last_syn + self._rtc_interval
twait = max(tend - time(), 5) # Prolonged outage
await asyn.sleep(twait)
self._time_valid = False
示例6: index
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep [as 别名]
def index(req, resp):
# parse query string
req.parse_qs()
flash = req.form.get('flash', 'false')
if flash == 'true':
led.on()
camera.init()
# wait for sensor to start and focus before capturing image
await asyncio.sleep(2)
buf = camera.capture()
led.off()
camera.deinit()
if len(buf) > 0:
yield from picoweb.start_response(resp, "image/jpeg")
yield from resp.awrite(buf)
else:
picoweb.http_error(resp, 503)
示例7: write
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep [as 别名]
def write(self, line, qos=True, wait=True):
if qos and wait:
while self._acks_pend:
await asyncio.sleep(TIM_TINY)
fstr = '{:02x}{}' if line.endswith('\n') else '{:02x}{}\n'
mid = next(self._getmid)
self._acks_pend.add(mid)
# ACK will be removed from ._acks_pend by ._read
line = fstr.format(mid, line) # Local copy
await self._vwrite(line) # Write verbatim
if not qos: # Don't care about ACK. All done.
return
# qos: pause until ACK received
while True:
await self._status_coro() # Wait for outage to clear
if await self._waitack(mid):
return # Got ack, removed from ._acks_pend, all done
# Either timed out or an outage started
await self._vwrite(line) # Waits for outage to clear
self._verbose and print('Repeat', line[2:], 'to server app')
# When ._read receives an ACK it is discarded from ._acks_pend. Wait for
# this to occur (or an outage to start). Currently use system timeout.
示例8: _vwrite
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep [as 别名]
def _vwrite(self, line):
ok = False
while not ok:
if self._verbose and not self():
print('Writer Client:', self._cl_id, 'awaiting OK status')
await self._status_coro()
if line is None:
line = '\n' # Keepalive. Send now: don't care about loss
else:
# Aawait client ready after initial or subsequent connection
while self._wr_pause:
await asyncio.sleep(self._tim_short)
async with self._wlock: # >1 writing task?
ok = await self._send(line) # Fail clears status
# Send a string. Return True on apparent success, False on failure.
示例9: run
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep [as 别名]
def run():
global success
ok = True
try:
while ok:
res = poller.ipoll(10)
for sock, ev in res:
if ev & select.POLLIN:
r = sock.readline()
print(ev, r)
# A server outage prints 1, b'' forever on ESP8266 or Unix.
# If killer closes socket on ESP8266 ev is always 1,
# on Unix get ev == 32
# Never see 9 or 17 (base 10) which are the error responses expected by uasyncio
# (POLLIN & POLLERR or POLLIN & POLLHUP)
else: # The only way I can make it work (on Unix) is to quit on 32
print('Terminating event:', ev) # What is 32??
ok = False
break
await asyncio.sleep(0)
except OSError:
print('Got OSError') # Never happens
success = True # Detected socket closure or error by OSError or event
示例10: run
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep [as 别名]
def run():
global success
ok = True
try:
while ok:
res = poller.ipoll(10)
for sock, ev in res:
if ev & select.POLLOUT:
r = sock.send(b'0123456789\n')
print(ev, r)
# On ESP8266 if another task closes the socket the poll object
# never triggers. uasyncio expects it to trigger with POLLHUP or
# (POLLOUT & POLLERR or POLLOUT & POLLHUP)
# If server fails gets OSError on both platforms.
else: # But on Unix socket closure produces ev == 32
print('Terminating event:', ev) # What is 32??
ok = False
break
await asyncio.sleep(1)
await asyncio.sleep(0)
except OSError:
print('Got OSError') # Happens on ESP8266 if server fails
success = True # Detected socket closure or error by OSError or event
示例11: run_ack
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep [as 别名]
def run_ack():
message = Message()
ack1 = Message()
ack2 = Message()
count = 0
while True:
asyncio.create_task(message_wait(message, ack1, 1))
asyncio.create_task(message_wait(message, ack2, 2))
message.set(count)
count += 1
print('message was set')
await ack1
ack1.clear()
print('Cleared ack1')
await ack2
ack2.clear()
print('Cleared ack2')
message.clear()
print('Cleared message')
await asyncio.sleep(1)
示例12: _run
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep [as 别名]
def _run(self):
while True:
v = int.from_bytes(uos.urandom(3),'little')/16777216
self.value(v, color(v))
self.led.color(color(v))
self.led.text(txt(v), fgcolor=color(v))
# Slow asynchronous data acquisition might occur here. Note
# that meters update themselves asynchronously (in a real
# application as data becomes available).
await asyncio.sleep(v) # Demo variable times
示例13: __aexit__
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep [as 别名]
def __aexit__(self, *args):
self.release()
await asyncio.sleep(0)
示例14: __init__
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep [as 别名]
def __init__(self, lp=False):
self.after = after if (p_version and lp) else asyncio.sleep
self.clear()
示例15: __call__
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep [as 别名]
def __call__(self):
return self.taskid
# Sleep coro breaks up a sleep into shorter intervals to ensure a rapid
# response to StopTask exceptions