summaryrefslogtreecommitdiff
path: root/tools/net/ynl/lib/ynl.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/net/ynl/lib/ynl.py')
-rw-r--r--tools/net/ynl/lib/ynl.py57
1 files changed, 35 insertions, 22 deletions
diff --git a/tools/net/ynl/lib/ynl.py b/tools/net/ynl/lib/ynl.py
index 92f85698c50e..01ec01a90e76 100644
--- a/tools/net/ynl/lib/ynl.py
+++ b/tools/net/ynl/lib/ynl.py
@@ -13,6 +13,7 @@ import yaml
import ipaddress
import uuid
import queue
+import selectors
import time
from .nlspec import SpecFamily
@@ -907,37 +908,49 @@ class YnlFamily(SpecFamily):
msg['msg'] = attrs
self.async_msg_queue.put(msg)
- def check_ntf(self, interval=0.1):
+ def check_ntf(self):
while True:
try:
reply = self.sock.recv(self._recv_size, socket.MSG_DONTWAIT)
- nms = NlMsgs(reply)
- self._recv_dbg_print(reply, nms)
- for nl_msg in nms:
- if nl_msg.error:
- print("Netlink error in ntf!?", os.strerror(-nl_msg.error))
- print(nl_msg)
- continue
- if nl_msg.done:
- print("Netlink done while checking for ntf!?")
- continue
+ except BlockingIOError:
+ return
- decoded = self.nlproto.decode(self, nl_msg, None)
- if decoded.cmd() not in self.async_msg_ids:
- print("Unexpected msg id while checking for ntf", decoded)
- continue
+ nms = NlMsgs(reply)
+ self._recv_dbg_print(reply, nms)
+ for nl_msg in nms:
+ if nl_msg.error:
+ print("Netlink error in ntf!?", os.strerror(-nl_msg.error))
+ print(nl_msg)
+ continue
+ if nl_msg.done:
+ print("Netlink done while checking for ntf!?")
+ continue
- self.handle_ntf(decoded)
- except BlockingIOError:
- pass
+ decoded = self.nlproto.decode(self, nl_msg, None)
+ if decoded.cmd() not in self.async_msg_ids:
+ print("Unexpected msg id while checking for ntf", decoded)
+ continue
+
+ self.handle_ntf(decoded)
+ def poll_ntf(self, duration=None):
+ start_time = time.time()
+ selector = selectors.DefaultSelector()
+ selector.register(self.sock, selectors.EVENT_READ)
+
+ while True:
try:
yield self.async_msg_queue.get_nowait()
except queue.Empty:
- try:
- time.sleep(interval)
- except KeyboardInterrupt:
- return
+ if duration is not None:
+ timeout = start_time + duration - time.time()
+ if timeout <= 0:
+ return
+ else:
+ timeout = None
+ events = selector.select(timeout)
+ if events:
+ self.check_ntf()
def operation_do_attributes(self, name):
"""