Python源码示例:asyncio.sleep_ms()
示例1
def coro(self, host, scr):
"""
Waits for one of two events:
- either user presses something on the screen
- or host finishes processing
Also updates progress screen
"""
while host.in_progress and scr.waiting:
await asyncio.sleep_ms(30)
scr.tick(5)
scr.set_progress(host.progress)
if host.in_progress:
host.abort()
if scr.waiting:
scr.waiting = False
await self.close_popup()
示例2
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.
示例3
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.')
示例4
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.')
示例5
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.
示例6
def query(self, data, timeout=100):
"""Blocking query"""
self.uart.write(data)
t0 = time.time()
while self.uart.any() < 7:
time.sleep_ms(10)
t = time.time()
if t > t0+timeout/1000:
return None
res = self.uart.read(7)
return res
示例7
def scan(self):
self.clean_uart()
if self.trigger is not None:
self.trigger.off()
else:
self.set_setting(SETTINGS_ADDR, SETTINGS_CONT_MODE)
self.data = b""
self.scanning = True
self.animated = False
while self.scanning:
await asyncio.sleep_ms(10)
# we will exit this loop from update()
# or manual cancel from GUI
return self.data
示例8
def update(self):
if not self.scanning:
self.clean_uart()
return
# read all available data
if self.uart.any() > 0:
d = self.uart.read()
self.data += d
# we got a full scan
if self.data.endswith(self.EOL):
# maybe two
chunks = self.data.split(self.EOL)
self.data = b""
try:
for chunk in chunks[:-1]:
if self.process_chunk(chunk):
self.stop_scanning()
break
# animated in trigger mode
elif self.trigger is not None:
self.trigger.on()
await asyncio.sleep_ms(30)
self.trigger.off()
except Exception as e:
print(e)
self.stop_scanning()
raise e
示例9
def update_loop(self, dt:int):
while not self.enabled:
await asyncio.sleep_ms(100)
while True:
if self.enabled:
try:
await self.update()
except Exception as e:
self.abort()
if self.manager is not None:
await self.manager.host_exception_handler(e)
# Keep await sleep here
# It allows other functions to run
await asyncio.sleep_ms(dt)
示例10
def enable(self):
"""
What should happen when host enables?
Maybe you want to remove all pending data first?
"""
if not self.initialized:
self.init()
await asyncio.sleep_ms(self.RECOVERY_TIME)
self.initialized = True
self.enabled = True
示例11
def load_screen(self, scr):
while self.background is not None:
await asyncio.sleep_ms(10)
old_scr = lv.scr_act()
lv.scr_load(scr)
self.scr = scr
old_scr.del_async()
示例12
def open_popup(self, scr):
# wait for another popup to finish
while self.background is not None:
await asyncio.sleep_ms(10)
self.background = self.scr
self.scr = scr
lv.scr_load(scr)
示例13
def update_loop(self, dt):
while True:
update(dt)
await asyncio.sleep_ms(dt)
示例14
def result(self):
self.waiting = True
while self.waiting:
await asyncio.sleep_ms(10)
return self.get_value()
示例15
def acquire(self):
while True:
if self._locked:
await asyncio.sleep_ms(self.delay_ms)
else:
self._locked = True
break
示例16
def __await__(self):
if upython:
while not self():
yield from asyncio.sleep_ms(self._tim_short_ms)
else:
# CPython: Meet requirement for generator in __await__
# https://github.com/python/asyncio/issues/451
yield from self._status_coro().__await__()
示例17
def __await__(self):
self._update()
if self._at_limit(): # All other threads are also at limit
if self._func is not None:
launch(self._func, self._args)
self._reset(not self._down) # Toggle direction to release others
return
direction = self._down
while True: # Wait until last waiting thread changes the direction
if direction != self._down:
return
await asyncio.sleep_ms(0)
示例18
def wait(self): # CPython comptaibility
while not self._flag:
await asyncio.sleep_ms(self.delay_ms)
示例19
def __await__(self):
while not self._flag:
await asyncio.sleep_ms(self.delay_ms)
示例20
def acquire(self):
while self._count == 0:
await asyncio.sleep_ms(0)
self._count -= 1
示例21
def acquire(self):
while True:
if self._locked:
await asyncio.sleep_ms(self.delay_ms)
else:
self._locked = True
break
示例22
def wait(self): # CPython comptaibility
while not self._flag:
await asyncio.sleep_ms(self.delay_ms)
示例23
def __await__(self):
while not self._flag:
await asyncio.sleep_ms(self.delay_ms)
示例24
def __await__(self):
self._update()
if self._at_limit(): # All other threads are also at limit
if self._func is not None:
launch(self._func, self._args)
self._reset(not self._down) # Toggle direction to release others
return
direction = self._down
while True: # Wait until last waiting thread changes the direction
if direction != self._down:
return
await asyncio.sleep_ms(0)
示例25
def acquire(self):
while self._count == 0:
await asyncio.sleep_ms(0)
self._count -= 1
示例26
def wait(self): # CPython comptaibility
while not self._flag:
await asyncio.sleep_ms(self.delay_ms)
示例27
def __await__(self):
while not self._flag:
await asyncio.sleep_ms(self.delay_ms)
示例28
def my_coro(text):
try:
await asyncio.sleep_ms(0)
while True:
await asyncio.sleep(1)
print(text)
except asyncio.CancelledError:
print('my_coro was cancelled.')
示例29
def bart():
barrier = Barrier(4, my_coro, ('my_coro running',))
for x in range(3):
asyncio.create_task(report1(barrier, x))
await barrier
# Must yield before reading result(). Here we wait long enough for
await asyncio.sleep_ms(1500) # coro to print
barrier.result().cancel()
await asyncio.sleep(2)
示例30
def acquire(self):
while True:
if self._locked:
await asyncio.sleep_ms(self.delay_ms)
else:
self._locked = True
break