Subject: Re: [PATCH net-next v1 2/2] tools/net/ynl: add async notification handling

From: Donald Hunter <[email-masked]>
Date: Tue, 12 Nov 2024 09:16:02 +0000

Jakub Kicinski <[email-masked]> writes:

> On Mon, 11 Nov 2024 11:06:18 +0000 Donald Hunter wrote:
>> > On Fri,  8 Nov 2024 12:38:16 +0000 Donald Hunter wrote:  
>> >> +    def poll_ntf(self, interval=0.1, duration=None):
>> >> +        endtime = time.time() + duration if duration else None  
>> >
>> > could we default duration to 0 and always check endtime?
>> > I think we can assume that time doesn't go back for simplicity  
>> 
>> I don't follow; what are you suggesting I initialise endtime to when
>> duration is 0 ?
>
> I was suggesting:
>
> 	def poll_nft([...], duration=0)
>
> 	endtime = time.time() + duration

I want it to run forever if a duration is not provided, but here
endtime == starttime so it would exit immediately.

I thought the original approach was fairly pythonic - if duration is not
specified (None) then there would be no endtime (None).

>> >> +        while True:
>> >> +            try:
>> >> +                self.check_ntf()
>> >> +                yield self.async_msg_queue.get_nowait()
>> >> +            except queue.Empty:
>> >> +                try:
>> >> +                    time.sleep(interval)  
>> >
>> > Maybe select or epoll would be better that periodic checks?  
>> 
>> This was the limit of my python knowledge TBH. I can try using python
>> selectors but I suspect periodic checks will still be needed to reliably
>> check the endtime.
>
> I thought select is pretty trivial to use in python, basically:
>
> 	sock, _, _ = select.select([sock], [], [], timeout=to)
> 	if sock:
> 		handle_sock()
> 	to = endtime - time.time()
> 	if to <= 0:
> 		return

Yep, thanks. I sketched out roughly this, but using the selectors module
which will use epoll under the covers.

https://docs.python.org/3/library/selectors.html