本文整理汇总了Python中uasyncio.CancelledError方法的典型用法代码示例。如果您正苦于以下问题:Python uasyncio.CancelledError方法的具体用法?Python uasyncio.CancelledError怎么用?Python uasyncio.CancelledError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类uasyncio
的用法示例。
在下文中一共展示了uasyncio.CancelledError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: acquire
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import CancelledError [as 别名]
def acquire(self):
if self._locked or self._awt:
# Lock set or just released but has tasks waiting on it,
# put the calling task on the Lock's waiting queue and yield
self.save_current()
try:
yield
except uasyncio.CancelledError:
if self._awt is uasyncio.cur_task:
# Task that was going to acquire got cancelled after being scheduled.
# Schedule next waiting task
self._locked = True
self.release()
raise
self._locked = True
return True
示例2: _wait_off
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import CancelledError [as 别名]
def _wait_off(self):
print("wait_off started")
st = time.ticks_ms()
try:
while time.ticks_ms() - st < self._on_time * 1000:
await asyncio.sleep(1)
except asyncio.CancelledError:
self._log.debug("_wait_off canceled", local_only=True)
print("wait_off canceled")
return
except Exception as e:
await self._log.asyncLog("error", "wait_off error: {!s}".format(e))
return False
finally:
print("wait_off exited")
await super().on_message(self._topic, "OFF", False)
示例3: _repeating
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import CancelledError [as 别名]
def _repeating(self):
print("repeating started")
self._repeating_mode = True
try:
while True:
st = time.ticks_ms()
await super().on_message(self._topic, "ON", False)
while time.ticks_ms() - st < self._on_time * 1000:
await asyncio.sleep(1)
await super().on_message(self._topic, "OFF", False)
st = time.ticks_ms()
while time.ticks_ms() - st < self._off_time * 1000:
await asyncio.sleep(1)
except asyncio.CancelledError:
print("repeating canceled")
self._log.debug("_repeating canceled", local_only=True)
except Exception as e:
await self._log.asyncLog("error", "_repeating error: {!s}".format(e))
finally:
await super().on_message(self._topic, "OFF", False)
self._repeating_mode = False
print("repeating exited")
示例4: _connected_handler
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import CancelledError [as 别名]
def _connected_handler(self, client):
try:
await self.publish(self.getDeviceTopic(config.MQTT_AVAILABILITY_SUBTOPIC), "online",
qos=1, retain=True)
# if it hangs here because connection is lost, it will get canceled when reconnected.
if self.__first_connect is True:
# only log on first connection, not on reconnect as nothing has changed here
await _log.asyncLog("info", str(os.name if platform == "linux" else os.uname()))
await _log.asyncLog("info", "Client version: {!s}".format(config.VERSION))
self.__first_connect = False
elif self.__first_connect is False:
await _log.asyncLog("debug", "Reconnected")
# resubscribe topics because clean session is used
if self._sub_coro is not None:
asyncio.cancel(self._sub_coro)
self._sub_coro = self._subscribeTopics()
asyncio.get_event_loop().create_task(self._sub_coro)
for cb in self._reconnected_subs:
res = cb(client)
if type(res) == type_gen:
await res
self._connected_coro = None
except asyncio.CancelledError:
if self._sub_coro is not None:
asyncio.cancel(self._sub_coro)
示例5: _repeating
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import CancelledError [as 别名]
def _repeating(self, component_on, component_off):
print("repeating started")
try:
while True:
st = time.ticks_ms()
await component_on()
while time.ticks_diff(time.ticks_ms(), st) < self._on_time * 1000:
await asyncio.sleep(0.2)
await component_off()
st = time.ticks_ms()
while time.ticks_diff(time.ticks_ms(), st) < self._off_time * 1000:
await asyncio.sleep(0.2)
except asyncio.CancelledError:
print("repeating canceled")
finally:
await component_off()
self._coro = None
print("repeating exited")
示例6: flash
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import CancelledError [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.
示例7: _tcp_server
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import CancelledError [as 别名]
def _tcp_server(self, host, port, backlog):
"""TCP Server implementation.
Opens socket for accepting connection and
creates task for every new accepted connection
"""
addr = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM)[0][-1]
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setblocking(False)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(addr)
sock.listen(backlog)
try:
while True:
yield asyncio.IORead(sock)
csock, caddr = sock.accept()
csock.setblocking(False)
# Start handler / keep it in the map - to be able to
# shutdown gracefully - by close all connections
self.processed_connections += 1
hid = id(csock)
handler = self._handler(asyncio.StreamReader(csock),
asyncio.StreamWriter(csock, {}))
self.conns[hid] = handler
self.loop.create_task(handler)
# In case of max concurrency reached - temporary pause server:
# 1. backlog must be greater than max_concurrency, otherwise
# client will got "Connection Reset"
# 2. Server task will be resumed whenever one active connection finished
if len(self.conns) == self.max_concurrency:
# Pause
yield False
except asyncio.CancelledError:
return
finally:
sock.close()
示例8: foo
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import CancelledError [as 别名]
def foo(n):
try:
while True:
await asyncio.sleep(0)
print(n)
except asyncio.CancelledError:
print('Task {} canned.'.format(n))
raise
示例9: _operationTimeout
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import CancelledError [as 别名]
def _operationTimeout(self, coro, *args, i):
try:
await coro(*args)
except asyncio.CancelledError:
raise # in case the calling function need to handle Cancellation too
finally:
self._ops_coros[i] = None
示例10: _preprocessor
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import CancelledError [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
示例11: _wait_off
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import CancelledError [as 别名]
def _wait_off(self, component_off):
print("wait_off started")
st = time.ticks_ms()
try:
while time.ticks_diff(time.ticks_ms(), st) < self._on_time * 1000:
await asyncio.sleep(0.2)
except asyncio.CancelledError:
print("wait_off canceled")
finally:
self._coro = None # prevents cancelling the cancelled coro
await component_off()
print("wait_off exited")
示例12: _watcher
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import CancelledError [as 别名]
def _watcher(self):
mqtt = _mqtt
mqtt.subscribeSync(self._topic, self.on_message, self)
try:
while True:
while mqtt.isconnected() is False:
await asyncio.sleep(1)
if await mqtt.awaitSubscriptionsDone(await_connection=False):
_log.debug("waiting for config", local_only=True)
await _mqtt.publish(
"{!s}/login/{!s}/set".format(mqtt.mqtt_home, mqtt.client_id),
[config.VERSION, platform, WAIT])
gc.collect()
else:
await asyncio.sleep(20)
continue
for _ in range(120):
if mqtt.isconnected() is False:
break
await asyncio.sleep(1)
# so that it can be cancelled properly every second
except asyncio.CancelledError:
if config.DEBUG is True:
_log.debug("_watcher cancelled", local_only=True)
except Exception as e:
await _log.asyncLog("error", "Error watching remoteConfig:", e)
finally:
await mqtt.unsubscribe(self._topic, self)
self._done = True
示例13: my_coro
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import CancelledError [as 别名]
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.')
示例14: foo
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import CancelledError [as 别名]
def foo(n):
print('Start timeout coro foo()')
try:
while True:
await asyncio.sleep(1)
n += 1
except asyncio.CancelledError:
print('Trapped foo timeout.')
raise
return n
示例15: bar
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import CancelledError [as 别名]
def bar(n):
print('Start cancellable bar()')
try:
while True:
await asyncio.sleep(1)
n += 1
except asyncio.CancelledError: # Demo of trapping
print('Trapped bar cancellation.')
raise
return n