Skip to contentSkip to conversation

tools/net/ynl: improve async notification handling

PATCH Applied Donald Hunter 3 participants 3 messages Oct 18, 2024 – Oct 24, 2024

Subsystems (3)
lore
Share & tools
lore

Donald Hunter

author maintainer
#0 lore raw

PATCH 1/1 tools/net/ynl: improve async notification handling

The notification handling in ynl is currently very simple, using sleep() to wait a period of time and then handling all the buffered messages in a single batch.

This patch changes the notification handling so that messages are processed as they are received. This makes it possible to use ynl as a library that supplies notifications in a timely manner.

- Change check_ntf() to be a generator that yields 1 notification at a time and blocks until a notification is available.
- Use the --sleep parameter to set an alarm and exit when it fires.

This means that the CLI has the same interface, but notifications get printed as they are received:

./tools/net/ynl/cli.py --spec <SPEC> --subscribe <TOPIC> [ --sleep <SECS> ]

Here is an example python snippet that shows how to use ynl as a library for receiving notifications:

ynl = YnlFamily(f"{dir}/rt_route.yaml")
ynl.ntf_subscribe('rtnlgrp-ipv4-route')

for event in ynl.check_ntf():
handle(event)

---
 tools/net/ynl/cli.py     | 10 +++++---
 tools/net/ynl/lib/ynl.py | 49 ++++++++++++++++++++++++----------------
 2 files changed, 36 insertions(+), 23 deletions(-)
tools/net/ynl/cli.py +7 -3
index b8481f401376..9e95016b85b3 100755
@@ -5,6 +5,7 @@ import argparse
 import json
 import pprint
 import time
+import signal
 
 from lib import YnlFamily, Netlink, NlError
 
@@ -17,6 +18,8 @@ class YnlEncoder(json.JSONEncoder):
             return list(obj)
         return json.JSONEncoder.default(self, obj)
 
+def handle_timeout(sig, frame):
+    exit(0)
 
 def main():
     description = """
@@ -81,7 +84,8 @@ def main():
         ynl.ntf_subscribe(args.ntf)
 
     if args.sleep:
-        time.sleep(args.sleep)
+        signal.signal(signal.SIGALRM, handle_timeout)
+        signal.alarm(args.sleep)
 
     if args.list_ops:
         for op_name, op in ynl.ops.items():
@@ -106,8 +110,8 @@ def main():
         exit(1)
 
     if args.ntf:
-        ynl.check_ntf()
-        output(ynl.async_msg_queue)
+        for msg in ynl.check_ntf():
+            output(msg)
 
 
 if __name__ == "__main__":
tools/net/ynl/lib/ynl.py +29 -20

74 lines not on this page · raw message

Signature · 1 line
2.47.0
Tested-by on v1 1/1
#1 lore raw
Quoted 17 lines · On Fri, 18 Oct 2024 10:32:28 +0100 Donald Hunter <[email protected]>…

The notification handling in ynl is currently very simple, using sleep() to wait a period of time and then handling all the buffered messages in a single batch.

This patch changes the notification handling so that messages are processed as they are received. This makes it possible to use ynl as a library that supplies notifications in a timely manner.

- Change check_ntf() to be a generator that yields 1 notification at a time and blocks until a notification is available.
- Use the --sleep parameter to set an alarm and exit when it fires.

This means that the CLI has the same interface, but notifications get printed as they are received:

./tools/net/ynl/cli.py --spec <SPEC> --subscribe <TOPIC> [ --sleep <SECS> ]

Quoted 8 lines

Here is an example python snippet that shows how to use ynl as a library for receiving notifications:

ynl = YnlFamily(f"{dir}/rt_route.yaml")
ynl.ntf_subscribe('rtnlgrp-ipv4-route')

for event in ynl.check_ntf():
handle(event)

Thank you!

Signature · 3 lines
Köry Maincent, Bootlin
Embedded Linux and kernel engineering
https://bootlin.com
#2 lore raw
In reply to Donald Hunter
This patch was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Quoted 9 lines · On Fri, 18 Oct 2024 10:32:28 +0100 you wrote:

The notification handling in ynl is currently very simple, using sleep() to wait a period of time and then handling all the buffered messages in a single batch.

This patch changes the notification handling so that messages are processed as they are received. This makes it possible to use ynl as a library that supplies notifications in a timely manner.

[...]

Here is the summary with links:
- [net-next,v1] tools/net/ynl: improve async notification handling

    https://git.kernel.org/netdev/net-next/c/1bf70e6c3a53

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html