From 7f0dce1f6f2f6a442041cde21694a86c6a28d599 Mon Sep 17 00:00:00 2001 From: Dmitry Volodin Date: Tue, 30 Jan 2024 18:17:50 +0100 Subject: [PATCH] sync client: Make iteration methods more reliable --- src/gufo/snmp/sync_client/getbulk.py | 7 ++++++- src/gufo/snmp/sync_client/getnext.py | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/gufo/snmp/sync_client/getbulk.py b/src/gufo/snmp/sync_client/getbulk.py index 5d217e7..a56a0bb 100755 --- a/src/gufo/snmp/sync_client/getbulk.py +++ b/src/gufo/snmp/sync_client/getbulk.py @@ -9,6 +9,7 @@ # Python modules +import time from typing import List, Optional, Tuple # Gufo Labs Modules @@ -58,9 +59,13 @@ def __next__(self: "GetBulkIter") -> Tuple[str, ValueType]: # Policer if self._policer: self._policer.wait_sync() + else: + # Kind of ancient dark magic to force + # a context switch and prevent EWOULDBLOCK + time.sleep(0.0) # try: - self.buffer = self._sock.sync_getbulk(self._ctx) + self._buffer = self._sock.sync_getbulk(self._ctx) except BlockingIOError as e: raise TimeoutError from e # End diff --git a/src/gufo/snmp/sync_client/getnext.py b/src/gufo/snmp/sync_client/getnext.py index d812d37..e4f4648 100755 --- a/src/gufo/snmp/sync_client/getnext.py +++ b/src/gufo/snmp/sync_client/getnext.py @@ -9,6 +9,7 @@ # Python modules +import time from typing import Optional, Tuple # Gufo Labs Modules @@ -47,6 +48,10 @@ def __next__(self: "GetNextIter") -> Tuple[str, ValueType]: """Get next value.""" if self._policer: self._policer.wait_sync() + else: + # Kind of ancient dark magic to force + # a context switch and prevent EWOULDBLOCK + time.sleep(0.0) try: return self._sock.sync_getnext(self._ctx) except StopAsyncIteration as e: