Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove pointless try/except in polling_task #2091

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions pymodbus/transport/serialtransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ def __init__(self, loop, protocol, *args, **kwargs) -> None:
self._poll_wait_time = 0.0005
self.sync_serial.timeout = 0
self.sync_serial.write_timeout = 0
self.future: asyncio.Task | None = None

def setup(self):
def setup(self) -> None:
"""Prepare to read/write."""
if os.name == "nt" or self.force_poll:
self.poll_task = asyncio.create_task(self.polling_task())
Expand All @@ -44,7 +43,6 @@ def close(self, exc: Exception | None = None) -> None:
self.flush()
if self.poll_task:
self.poll_task.cancel()
self.future = asyncio.ensure_future(self.poll_task)
self.poll_task = None
else:
self.async_loop.remove_reader(self.sync_serial.fileno())
Expand Down Expand Up @@ -147,16 +145,12 @@ def intern_write_ready(self) -> None:

async def polling_task(self):
"""Poll and try to read/write."""
try:
while self.sync_serial:
await asyncio.sleep(self._poll_wait_time)
while self.intern_write_buffer:
self.intern_write_ready()
if self.sync_serial.in_waiting:
self.intern_read_ready()
except asyncio.CancelledError:
self.close(None)

while self.sync_serial:
await asyncio.sleep(self._poll_wait_time)
while self.intern_write_buffer:
self.intern_write_ready()
if self.sync_serial.in_waiting:
self.intern_read_ready()

async def create_serial_connection(
loop, protocol_factory, *args, **kwargs
Expand Down
4 changes: 3 additions & 1 deletion test/transport/test_serial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Test transport."""
import asyncio
import contextlib
import os
from functools import partial
from unittest import mock
Expand Down Expand Up @@ -114,7 +115,8 @@ async def test_polling(self):
comm = SerialTransport(asyncio.get_running_loop(), mock.Mock(), "dummy")
comm.sync_serial = mock.MagicMock()
comm.sync_serial.read.side_effect = asyncio.CancelledError("test")
await comm.polling_task()
with contextlib.suppress(asyncio.CancelledError):
await comm.polling_task()

@pytest.mark.skipif(os.name == "nt", reason="Windows not supported")
async def test_poll_task(self):
Expand Down