本文整理汇总了Python中uasyncio.cancel方法的典型用法代码示例。如果您正苦于以下问题:Python uasyncio.cancel方法的具体用法?Python uasyncio.cancel怎么用?Python uasyncio.cancel使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类uasyncio
的用法示例。
在下文中一共展示了uasyncio.cancel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: changeMode
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import cancel [as 别名]
def changeMode(self, topic, msg, retain):
print("changeMode", topic, msg, retain, self._repeating_mode)
if msg not in _mqtt.payload_on and msg not in _mqtt.payload_off:
raise ValueError("unsupported payload {!r}".format(msg))
if msg in _mqtt.payload_on:
if self._repeating_mode is True:
# already on
return True
elif self._repeating_mode is False:
await super().on_message(self._topic, "OFF", retain)
self._off_coro = self._repeating()
asyncio.get_event_loop().create_task(self._off_coro)
elif msg in _mqtt.payload_off:
if self._off_coro is not None:
asyncio.cancel(self._off_coro) # will shut down pump
self._off_coro = None
if self._repeating_mode is True:
return True
elif self._repeating_mode is False:
await super().on_message(self._topic, "OFF", retain)
return True
return True
示例2: _connected_handler
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import cancel [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)
示例3: bar
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import cancel [as 别名]
def bar(loop):
foo1 = foo(1)
foo5 = foo(5)
lpfoo1 = lpfoo(1)
lpfoo5 = lpfoo(5)
loop.create_task(foo1)
loop.create_task(foo5)
loop.create_task(lpfoo1)
loop.create_task(lpfoo5)
await asyncio.sleep(2)
print('Cancelling tasks')
asyncio.cancel(foo1)
asyncio.cancel(foo5)
asyncio.cancel(lpfoo1)
asyncio.cancel(lpfoo5)
await asyncio.sleep(0) # Allow cancellation to occur
print('Pausing 7s to ensure no task still running.')
await asyncio.sleep(7)
print('Launching tasks with 2s timeout')
loop.create_task(run(foo(1), 2))
loop.create_task(run(lpfoo(1), 2))
loop.create_task(run(foo(20), 2))
loop.create_task(run(lpfoo(20), 2))
print('Pausing 7s to ensure no task still running.')
await asyncio.sleep(7)
示例4: _cancel
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import cancel [as 别名]
def _cancel(cls, task_no):
task = cls.tasks[task_no][0]
asyncio.cancel(task)
示例5: cancel
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import cancel [as 别名]
def cancel(cls, name, nowait=True):
if name in cls.instances:
await cls.cancel_all(group=name, nowait=nowait)
return True
return False
示例6: shutdown
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import cancel [as 别名]
def shutdown(self):
"""Gracefully shutdown Web Server"""
asyncio.cancel(self._server_coro)
for hid, coro in self.conns.items():
asyncio.cancel(coro)
示例7: on_message
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import cancel [as 别名]
def on_message(self, topic, msg, retain):
"""
Changes the state of the pump.
In switch mode a safety shutdown coro will be started.
In repeating mode only the state of the pump gets changed.
"""
print("on_message", topic, msg, retain, self._repeating_mode)
if retain is True:
await asyncio.sleep(2) # so that other retained message about on_time,off_time and mode get processed first
if self._repeating_mode is False:
if self._off_coro is not None:
asyncio.cancel(self._off_coro)
if msg in _mqtt.payload_on:
if (await super().on_message(topic, msg, retain)) is True:
if self._repeating_mode is False:
self._off_coro = self._wait_off()
asyncio.get_event_loop().create_task(self._off_coro)
elif msg in _mqtt.payload_off:
if (await super().on_message(topic, msg, retain)) is False:
if self._repeating_mode is False:
self._off_coro = self._wait_off()
asyncio.get_event_loop().create_task(self._off_coro) # try again
else:
await _log.asyncLog("error", "unsupported payload: {!s}".format(msg))
return False
return True
示例8: _wifiChanged
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import cancel [as 别名]
def _wifiChanged(self, state):
if self._wifi_coro is not None:
asyncio.cancel(self._wifi_coro)
self._wifi_coro = self._wifi_changed(state)
asyncio.get_event_loop().create_task(self._wifi_coro)
示例9: _connected
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import cancel [as 别名]
def _connected(self, client):
_log.debug("mqtt connected", local_only=True)
self.__reconnects += 1
if self.__last_disconnect is not None:
self.__downtime += (time.ticks_ms() - self.__last_disconnect) / 1000
self.__last_disconnect = None
if self._connected_coro is not None:
# processed subscriptions would have to be done again anyway
asyncio.cancel(self._connected_coro)
self._connected_coro = self._connected_handler(client)
asyncio.get_event_loop().create_task(self._connected_coro)
示例10: on
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import cancel [as 别名]
def on(self):
"""Turn switch on. Can be used by other components to control this component"""
if self.lock.locked() is True and self._wfl is False:
return False
async with self.lock:
res = await self._on() # if _on() returns True the value should be published
if res is True:
self._setState(True)
if self._pub_task:
asyncio.cancel(self._pub_task)
self._pub_task = None
self._pub_task = asyncio.get_event_loop().create_task(self.__publish("ON"))
return res
示例11: off
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import cancel [as 别名]
def off(self):
"""Turn switch off. Can be used by other components to control this component"""
if self.lock.locked() is True and self._wfl is False:
return False
async with self.lock:
res = await self._off() # if _off() returns True the value should be published
if res is True:
self._setState(False)
if self._pub_task:
asyncio.cancel(self._pub_task)
self._pub_task = None
self._pub_task = asyncio.get_event_loop().create_task(self.__publish("OFF"))
return res
示例12: _remove
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import cancel [as 别名]
def _remove(self):
"""Called by component base class when a sensor component should be removed"""
if self._loop_coro is not None:
asyncio.cancel(self._loop_coro)
await super()._remove()
示例13: off
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import cancel [as 别名]
def off(self, extended_switch, component, component_on, component_off):
"""Turn device off"""
if self._coro is not None:
asyncio.cancel(self._coro)
else:
await component_off()
return True
示例14: deactivate
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import cancel [as 别名]
def deactivate(self, extended_switch, component, component_on, component_off):
"""Triggered whenever the mode changes and this mode has been deactivated"""
asyncio.cancel(self._coro)
return True
示例15: on_message
# 需要导入模块: import uasyncio [as 别名]
# 或者: from uasyncio import cancel [as 别名]
def on_message(self, topic, msg, retain):
if retain is True:
return False
m = memoryview(topic)
if m[-4:] == b"/set":
return False
if m == memoryview(self._topic)[:-2]:
print("received amount", msg)
self._icomp = int(msg)
# no return so it can end if 0 components are expected
elif self._icomp is None:
await _log.asyncLog("error", "Need amount of components first")
return False
else:
if type(msg) != dict:
await _log.asyncLog("error", "Received config is no dict")
return False
name = topic[topic.rfind("/") + 1:]
del topic
gc.collect()
_log.info("received config for component", name, ":", msg, local_only=True)
if name in self._rcomp:
# received config already, typically happens if process was
# interrupted by network error
return False
self._rcomp.append(name)
self._saveComponent(name, msg)
await config.registerComponent(name, msg)
if len(self._rcomp) == self._icomp: # received all components
asyncio.cancel(self._watcher_coro)
return False