David Blume's GitList
Repositories
python_pinger.git
Code
Commits
Branches
Tags
Search
Tree:
7019525
Branches
Tags
main
python_pinger.git
README.md
Update the README.md with better links to images.
David Blume
commited
7019525
at 2019-07-05 20:33:19
README.md
Blame
History
Raw
# python_pinger These are different approaches to how one might implement a daemon to repeatedly ping multiple hosts to see if the network is up. ## Single Threaded This is the most naive implementation. For each ping host, the main process thread pings them one at a time, and prints any changes. ![Single Threaded Pings](http://dlma.com/images/python_pinger/ping_single_threaded.png) It's so simple that I'm not going to provide a sample source file, but for the others below, I will. ### Upsides Really simple code. ### Downsides Since the pings are serialized, one long timeout from one host could affect detecting a problem at another host. ## Long Lived Workers Consuming from Queue Raymond Hettinger's [PyBay 2018 Keynote](https://pybay.com/site_media/slides/raymond2017-keynote/threading.html) uses the queue module to send data between threads, so I thought I'd make a version of the pinger that did the same. ![Long Lived Queue Workers](http://dlma.com/images/python_pinger/ping_long_lived_queue_workers.png) See the source: **[long\_lived\_worker\_queue.py](http://git.dlma.com/python_pinger.git/blob/master/long_lived_worker_queue.py)** ### Upsides Multi-threaded ping calls won't block each other. ### Downsides The ping tasks read from, and write to a shared dictionary. (Reading and updating the last status.) ## Short Lived Workers How about we don't keep the workers around, and only spawn them when there's something to do? That way, we won't waste memory when there's nothing going on. ![Short Lived Workers](http://dlma.com/images/python_pinger/ping_short_lived_workers.png) See the source: **[short\_lived\_workers.py](http://git.dlma.com/python_pinger.git/blob/master/short_lived_workers.py)** ### Upsides The worker tasks aren't in memory if they're not doing anything. So usually a smaller memory profile. ### Downsides The ping tasks still read from, and write to a shared dictionary. (Reading and updating the last status.) ## Long Lived Looping Workers I saved the best for last. The only thing the main thread does is bring the workers and the print manager to life. The workers each independently do their own loop: ping, compare, print, and wait. ![Long Lived Looping Workers](http://dlma.com/images/python_pinger/ping_long_lived_looping_workers.png) See the source: **[long\_lived\_looping\_workers.py](http://git.dlma.com/python_pinger.git/blob/master/long_lived_looping_workers.py)** ### Upsides No more race conditions! The worker threads mind their own business. ### Downsides The worker threads remain in memory. ## Is it any good? [Yes](https://news.ycombinator.com/item?id=3067434). ## Licence This software uses the [MIT license](http://git.dlma.com/python_pinger.git/blob/master/LICENSE.txt).