本文整理汇总了Python中uasyncio.sleep_ms方法的典型用法代码示例。如果您正苦于以下问题:Python uasyncio.sleep_ms方法的具体用法?Python uasyncio.sleep_ms怎么用?Python uasyncio.sleep_ms使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类uasyncio
的用法示例。
在下文中一共展示了uasyncio.sleep_ms方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sleep
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep_ms [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: _as_read
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep_ms [as 别名]
def _as_read(self, n, sock=None): # OSError caught by superclass
if sock is None:
sock = self._sock
data = b''
t = ticks_ms()
while len(data) < n:
if self._timeout(t) or not self.isconnected():
raise OSError(-1)
try:
msg = sock.read(n - len(data))
except OSError as e: # ESP32 issues weird 119 errors here
msg = None
if e.args[0] not in BUSY_ERRORS:
raise
if msg == b'': # Connection closed by host
raise OSError(-1)
if msg is not None: # data received
data = b''.join((data, msg))
t = ticks_ms()
self.last_rx = ticks_ms()
await asyncio.sleep_ms(_SOCKET_POLL_DELAY)
return data
示例3: _as_write
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep_ms [as 别名]
def _as_write(self, bytes_wr, length=0, sock=None):
if sock is None:
sock = self._sock
if length:
bytes_wr = bytes_wr[:length]
t = ticks_ms()
while bytes_wr:
if self._timeout(t) or not self.isconnected():
raise OSError(-1)
try:
n = sock.write(bytes_wr)
except OSError as e: # ESP32 issues weird 119 errors here
n = 0
if e.args[0] not in BUSY_ERRORS:
raise
if n:
t = ticks_ms()
bytes_wr = bytes_wr[n:]
await asyncio.sleep_ms(_SOCKET_POLL_DELAY)
示例4: broker_up
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep_ms [as 别名]
def broker_up(self): # Test broker connectivity
if not self.isconnected():
return False
tlast = self.last_rx
if ticks_diff(ticks_ms(), tlast) < 1000:
return True
try:
await self._ping()
except OSError:
return False
t = ticks_ms()
while not self._timeout(t):
await asyncio.sleep_ms(100)
if ticks_diff(self.last_rx, tlast) > 0: # Response received
return True
return False
示例5: start
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep_ms [as 别名]
def start(self, user_task=None, awaitable=None):
loop = asyncio.get_event_loop()
while True:
if not self._running: # Restarting
self.lstrx = [] # Clear down queues
self.lsttx = []
self._synchronised = False
loop.create_task(self._run()) # Reset target (if possible)
while not self._synchronised: # Wait for sync
await asyncio.sleep_ms(100)
if user_task is None:
while self._running:
await asyncio.sleep_ms(100)
else:
await user_task(self) # User task must quit on timeout
# If it quit for other reasons force a t/o exception
self.stop()
await asyncio.sleep_ms(0)
if awaitable is not None: # User code may use an ExitGate
await awaitable # to ensure all coros have quit
# Can be used to force a failure
示例6: _send
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep_ms [as 别名]
def _send(self, d): # Write a line to socket.
async with self._s_lock:
start = utime.ticks_ms()
while d:
try:
ns = self._sock.send(d) # OSError if client closes socket
except OSError as e:
err = e.args[0]
if err == errno.EAGAIN: # Would block: await server read
await asyncio.sleep_ms(100)
else:
self._verbose and print('_send fail. Disconnect')
self._evfail.set()
return False # peer disconnect
else:
d = d[ns:]
if d: # Partial write: pause
await asyncio.sleep_ms(20)
if utime.ticks_diff(utime.ticks_ms(), start) > self._to:
self._verbose and print('_send fail. Timeout.')
self._evfail.set()
return False
self._last_wr = utime.ticks_ms()
return True
示例7: cond_go
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep_ms [as 别名]
def cond_go():
ntasks = 7
barrier = Barrier(ntasks + 1)
t1 = asyncio.create_task(cond01())
t3 = asyncio.create_task(cond03())
for n in range(ntasks):
asyncio.create_task(cond02(n, barrier))
await barrier # All instances of cond02 have completed
# Test wait_for
barrier = Barrier(2)
asyncio.create_task(cond04(99, barrier))
await barrier
# cancel continuously running coros.
t1.cancel()
t3.cancel()
await asyncio.sleep_ms(0)
print('Done.')
示例8: readline
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep_ms [as 别名]
def readline(s, timeout):
line = b''
start = utime.ticks_ms()
while True:
if line.endswith(b'\n'):
if len(line) > 1:
return line
line = b''
start = utime.ticks_ms() # A blank line is just a keepalive
await asyncio.sleep_ms(100) # See note above
d = s.readline()
if d == b'':
raise OSError
if d is not None:
line = b''.join((line, d))
if utime.ticks_diff(utime.ticks_ms(), start) > timeout:
raise OSError
示例9: readline
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep_ms [as 别名]
def readline(self):
line = b''
start = utime.ticks_ms()
while True:
if line.endswith(b'\n'):
if len(line) > 1:
return line
line = b''
start = utime.ticks_ms() # Blank line is keepalive
self.led(not self.led())
await asyncio.sleep_ms(100) # nonzero wait seems empirically necessary
d = self.sock.readline()
if d == b'':
raise OSError
if d is not None:
line = b''.join((line, d))
if utime.ticks_diff(utime.ticks_ms(), start) > self.timeout:
raise OSError
示例10: meter
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep_ms [as 别名]
def meter(n, x, text, t):
print('Meter {} test.'.format(n))
m = Meter(wri, 5, x, divisions = 4, ptcolor=YELLOW,
label=text, style=Meter.BAR, legends=('0.0', '0.5', '1.0'))
l = LED(wri, ssd.height - 16 - wri.height, x, bdcolor=YELLOW, label ='over')
while True:
v = int.from_bytes(uos.urandom(3),'little')/16777216
m.value(v, color(v))
l.color(color(v))
l.text(txt(v), fgcolor=color(v))
refresh(ssd)
await asyncio.sleep_ms(t)
示例11: flash
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep_ms [as 别名]
def flash(n, t):
led = pyb.LED(n)
while True:
led.toggle()
await asyncio.sleep_ms(t)
示例12: killer
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep_ms [as 别名]
def killer(tasks):
sw = pyb.Switch()
while not sw():
await asyncio.sleep_ms(100)
for task in tasks:
task.cancel()
示例13: flash
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep_ms [as 别名]
def flash(n, t):
led = pyb.LED(n)
try:
while True:
led.toggle()
await asyncio.sleep_ms(t)
except asyncio.CancelledError:
led.off() # Demo tidying up on cancellation.
示例14: wait
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep_ms [as 别名]
def wait(self, t):
while t >= 0:
if self.sw():
return True
await asyncio.sleep_ms(50)
t -= 50
return False
# The main task instantiates other tasks then does the display update process.
示例15: acquire
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import sleep_ms [as 别名]
def acquire(self):
while True:
if self._locked:
await asyncio.sleep_ms(self.delay_ms)
else:
self._locked = True
break