pythonping icon indicating copy to clipboard operation
pythonping copied to clipboard

Async Ping

Open volitank opened this issue 3 years ago • 9 comments

Hello,

I'm working on a project which currently utilizes your ping program. For one function that we do we issue quite a lot of pings which we are currently doing threaded.

We would like to switch this implementation to async if at all possible. So I'm wondering how difficult this would be to implement. or if you already know of a way.

I know there are other ping implementations that are async, but our program already works with yours, Your license is better, and we'll need to be including pythonping in with our source since there isn't a Debian/Ubuntu package at the moment.

Thanks, Blake

volitank avatar Feb 15 '22 22:02 volitank

By this you mean spawning a thread for a single ping and triggering some other code asynchronously when the ping completes? This should be already be possible because the library is thread-safe.

alessandromaggio avatar Feb 20 '22 16:02 alessandromaggio

No I mean pinging with asyncio instead of threads. I want to do a lot of pings simultaneously. Asyncio is a better method for this than opening hundreds of threads.

volitank avatar Feb 21 '22 23:02 volitank

This is is a great feature and can make the library significantly more useful and more modern. We need to get it done at some point.

alessandromaggio avatar Jul 29 '22 06:07 alessandromaggio

That's great to hear! Your library is the best for natively pinging in python.

volitank avatar Jul 29 '22 19:07 volitank

Hey @alessandromaggio

I take this issue is still open? I will be working on 'Cisco like output' bug as soon as I get the go ahead. Maybe I can take a crack at this one afterwords as well. This OS library will make for a very interesting weekend. I am looking forward to it :100:

AleksaZatezalo avatar Sep 28 '22 21:09 AleksaZatezalo

Hey @alessandromaggio & @volitank

I am currently reading up on asyncio - it seems simple enough but getting your hands dirty isn't easy. If I understand correctly implementing this asynchronously means altering the executor class and awaiting in the main init file. Can anyone confirm?

This is my first crack at asyncio. Some help would be nice :)

AleksaZatezalo avatar Nov 07 '22 01:11 AleksaZatezalo

Hey @AleksaZatezalo,

Yes, specifically it means when triggering a ping either from the executor or from the ping function itself we have the possibility to await for it. It would be nice to specify some parameter in ping() so that we maintain backward compatibility, for example mode="async" as opposed to a default mode="sync".

alessandromaggio avatar Nov 15 '22 07:11 alessandromaggio

Hey,

I have been researching this. It seems like we can't change mode so easily -- at least without making the ping function in init.py async. As it stands we cant call async functions from sync code.

This will cause a whole host of problems in dependencies down the line. Any thoughts? I am trying to go down the root of creating an abstract factory that creates an async ping.

AleksaZatezalo avatar Mar 01 '23 06:03 AleksaZatezalo

I bumped in this library and found it very useful, and it would be definitely more useful if it provides asynchronous I/O.

The low-level interfaces seem to have already provided relevant APIs: https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.sock_sendto

As it stands we cant call async functions from sync code.

@AleksaZatezalo can we convert the existing implementation to the asyncio style, and just call asyncio.run in the ping function?

Also to better support other use cases, an async ping API and an async iterator could be provided. The async ping API is mainly used by clients which have an existing asyncio context, and the async iterator could be used to stream results to clients.

haha454 avatar May 07 '23 03:05 haha454