How should I cancel do_reconnect() task in ModbusProtocol ? #2256
Answered
by
janiversen
realrushen
asked this question in
Help
-
Im using AsyncModbusTcpClient from async def run(self):
while True:
while not self.plc_client.connected:
self.plc_client.reconnect_task = asyncio.create_task(self.plc_client.do_reconnect())
await self.plc_client.reconnect_task
request = await self.queue.get()
if request is None:
self.queue.task_done()
break
try:
await request.execute(self.plc_client, self.response_queue, self.plc_id, self.slave_id)
except asyncio.CancelledError:
if self.plc_client.reconnect_task is not None:
await cansel_tasks([self.plc_client.reconnect_task])
raise
except Exception: # noqa
logger.error('error', exc_info=True)
self.queue.task_done() And when I want to shutdown my app I am canceling all my running tasks with this corutine: async def cansel_tasks(tasks: List[asyncio.Task]):
for task in tasks:
try:
if task.cancelled() or task.done():
continue
else:
task.cancel()
await task
except asyncio.CancelledError:
logger.debug('%s: canceled', task.get_name()) All fine when client connected, but when it trying to reconnect with do_reconnect task that ignoring # class ModbusProtocol
async def do_reconnect(self) -> None:
"""Handle reconnect as a task."""
try:
self.reconnect_delay_current = self.comm_params.reconnect_delay or 0.0
while True:
Log.debug(
"Wait {} {} ms before reconnecting.",
self.comm_params.comm_name,
self.reconnect_delay_current * 1000,
)
await asyncio.sleep(self.reconnect_delay_current)
if await self.connect():
break
self.reconnect_delay_current = min(
2 * self.reconnect_delay_current,
self.comm_params.reconnect_delay_max,
)
except asyncio.CancelledError:
pass
self.reconnect_task = None How should I cancel it properly? Or maybe I need to use different approach for reconnect? |
Beta Was this translation helpful? Give feedback.
Answered by
janiversen
Jul 22, 2024
Replies: 1 comment
-
disable the reconnect, and do it in your own code. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
janiversen
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
disable the reconnect, and do it in your own code.