|
12 | 12 | import yaml
|
13 | 13 | import ipaddress
|
14 | 14 | import uuid
|
| 15 | +import queue |
| 16 | +import time |
15 | 17 |
|
16 | 18 | from .nlspec import SpecFamily
|
17 | 19 |
|
@@ -489,7 +491,7 @@ def __init__(self, def_path, schema=None, process_unknown=False,
|
489 | 491 | self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_GET_STRICT_CHK, 1)
|
490 | 492 |
|
491 | 493 | self.async_msg_ids = set()
|
492 |
| - self.async_msg_queue = [] |
| 494 | + self.async_msg_queue = queue.Queue() |
493 | 495 |
|
494 | 496 | for msg in self.msgs.values():
|
495 | 497 | if msg.is_async:
|
@@ -903,32 +905,39 @@ def handle_ntf(self, decoded):
|
903 | 905 |
|
904 | 906 | msg['name'] = op['name']
|
905 | 907 | msg['msg'] = attrs
|
906 |
| - self.async_msg_queue.append(msg) |
| 908 | + self.async_msg_queue.put(msg) |
907 | 909 |
|
908 |
| - def check_ntf(self): |
| 910 | + def check_ntf(self, interval=0.1): |
909 | 911 | while True:
|
910 | 912 | try:
|
911 | 913 | reply = self.sock.recv(self._recv_size, socket.MSG_DONTWAIT)
|
912 |
| - except BlockingIOError: |
913 |
| - return |
| 914 | + nms = NlMsgs(reply) |
| 915 | + self._recv_dbg_print(reply, nms) |
| 916 | + for nl_msg in nms: |
| 917 | + if nl_msg.error: |
| 918 | + print("Netlink error in ntf!?", os.strerror(-nl_msg.error)) |
| 919 | + print(nl_msg) |
| 920 | + continue |
| 921 | + if nl_msg.done: |
| 922 | + print("Netlink done while checking for ntf!?") |
| 923 | + continue |
914 | 924 |
|
915 |
| - nms = NlMsgs(reply) |
916 |
| - self._recv_dbg_print(reply, nms) |
917 |
| - for nl_msg in nms: |
918 |
| - if nl_msg.error: |
919 |
| - print("Netlink error in ntf!?", os.strerror(-nl_msg.error)) |
920 |
| - print(nl_msg) |
921 |
| - continue |
922 |
| - if nl_msg.done: |
923 |
| - print("Netlink done while checking for ntf!?") |
924 |
| - continue |
| 925 | + decoded = self.nlproto.decode(self, nl_msg, None) |
| 926 | + if decoded.cmd() not in self.async_msg_ids: |
| 927 | + print("Unexpected msg id while checking for ntf", decoded) |
| 928 | + continue |
925 | 929 |
|
926 |
| - decoded = self.nlproto.decode(self, nl_msg, None) |
927 |
| - if decoded.cmd() not in self.async_msg_ids: |
928 |
| - print("Unexpected msg id done while checking for ntf", decoded) |
929 |
| - continue |
| 930 | + self.handle_ntf(decoded) |
| 931 | + except BlockingIOError: |
| 932 | + pass |
930 | 933 |
|
931 |
| - self.handle_ntf(decoded) |
| 934 | + try: |
| 935 | + yield self.async_msg_queue.get_nowait() |
| 936 | + except queue.Empty: |
| 937 | + try: |
| 938 | + time.sleep(interval) |
| 939 | + except KeyboardInterrupt: |
| 940 | + return |
932 | 941 |
|
933 | 942 | def operation_do_attributes(self, name):
|
934 | 943 | """
|
|
0 commit comments