b2f249cce67e0494465edeae7dd7e5bdae6b50dc
David Blume first commit.

David Blume authored 5 years ago

1) # python_pinger
2) 
3) These are different approaches to how one might implement a daemon to
4) repeatedly ping multiple hosts to see if the network is up.
5) 
6) ## Single Threaded
7) 
8) This is the most naive implementation. For each ping host, the main
9) process thread pings them one at a time, and prints any changes.
10) 
David Blume Update the README.md with b...

David Blume authored 5 years ago

11) ![Single Threaded Pings](http://dlma.com/images/python_pinger/ping_single_threaded.png)
12) 
David Blume Update the README.md a litt...

David Blume authored 5 years ago

13) It's so simple that I'm not going to provide a sample source file,
14) but for the others below, I will.
David Blume first commit.

David Blume authored 5 years ago

15) 
16) ### Upsides
17) 
18) Really simple code.
19) 
20) ### Downsides
21) 
22) Since the pings are serialized, one long timeout from one host could
23) affect detecting a problem at another host.
24) 
25) ## Long Lived Workers Consuming from Queue
26) 
27) Raymond Hettinger's [PyBay 2018 Keynote](https://pybay.com/site_media/slides/raymond2017-keynote/threading.html)
David Blume Update the README.md a litt...

David Blume authored 5 years ago

28) uses the queue module to send data between threads, so I thought I'd make a
29) version of the pinger that did the same.
David Blume first commit.

David Blume authored 5 years ago

30) 
David Blume Update the README.md with b...

David Blume authored 5 years ago

31) ![Long Lived Queue Workers](http://dlma.com/images/python_pinger/ping_long_lived_queue_workers.png)
David Blume first commit.

David Blume authored 5 years ago

32) 
David Blume Update the README.md a litt...

David Blume authored 5 years ago

33) The main thread sends an address to a queue that the worker threads wait upon.
34) 
David Blume Update the README.md with b...

David Blume authored 5 years ago

35) See the source: **[long\_lived\_worker\_queue.py](http://git.dlma.com/python_pinger.git/blob/master/long_lived_worker_queue.py)**
David Blume first commit.

David Blume authored 5 years ago

36) 
37) ### Upsides
38) 
39) Multi-threaded ping calls won't block each other.
40) 
41) ### Downsides
42) 
David Blume Update the README.md with b...

David Blume authored 5 years ago

43) The ping tasks read from, and write to a shared dictionary. (Reading and
44) updating the last status.)
David Blume first commit.

David Blume authored 5 years ago

45) 
46) ## Short Lived Workers
47) 
David Blume Add a version that uses a l...

David Blume authored 5 years ago

48) How about we don't keep the workers around, and only spawn them when
David Blume Update the README.md with b...

David Blume authored 5 years ago

49) there's something to do? That way, we won't waste memory when there's
David Blume Update the README.md a litt...

David Blume authored 5 years ago

50) nothing going on. The main thread passes the address to the workers when
51) they're constructed.
David Blume Update the README.md with b...

David Blume authored 5 years ago

52) 
53) ![Short Lived Workers](http://dlma.com/images/python_pinger/ping_short_lived_workers.png)
David Blume first commit.

David Blume authored 5 years ago

54) 
David Blume Update the README.md with b...

David Blume authored 5 years ago

55) See the source: **[short\_lived\_workers.py](http://git.dlma.com/python_pinger.git/blob/master/short_lived_workers.py)**
David Blume first commit.

David Blume authored 5 years ago

56) 
57) ### Upsides
58) 
59) The worker tasks aren't in memory if they're not doing anything. So usually a smaller memory profile.
60) 
61) ### Downsides
62) 
63) The ping tasks still read from, and write to a shared dictionary. (Reading and updating the last status.)
64) 
65) ## Long Lived Looping Workers
66) 
David Blume Update the README.md with b...

David Blume authored 5 years ago

67) I saved the best for last. The only thing the main thread does is bring the
68) workers and the print manager to life. The workers each independently do their own loop:
69) ping, compare, print, and wait.
70) 
David Blume Instead of having the proce...

David Blume authored 5 years ago

71) Since the process thread doesn't have anything to do after spawning the workers,
72) it can be one of the workers.
73) 
David Blume Update the README.md with b...

David Blume authored 5 years ago

74) ![Long Lived Looping Workers](http://dlma.com/images/python_pinger/ping_long_lived_looping_workers.png)
David Blume first commit.

David Blume authored 5 years ago

75) 
David Blume Update the README.md with b...

David Blume authored 5 years ago

76) See the source: **[long\_lived\_looping\_workers.py](http://git.dlma.com/python_pinger.git/blob/master/long_lived_looping_workers.py)**
David Blume first commit.

David Blume authored 5 years ago

77) 
78) ### Upsides
79) 
80) No more race conditions! The worker threads mind their own business.
81) 
82) ### Downsides
83) 
84) The worker threads remain in memory.
85) 
David Blume Add a version that uses a l...

David Blume authored 5 years ago

86) ## Long Lived Looping Locked Workers
87) 
88) That was fun using only the synchronized queue class and no locks. But now that
89) we've got the long lived looping workers that don't need their own queue, let's
90) replace the print manager with a threading lock.
91) 
92) ![Long Lived Looping Locked Workers](http://dlma.com/images/python_pinger/ping_long_lived_looping_locked_workers.png)
93) 
94) See the source: **[long\_lived\_looping\_locked\_workers.py](http://git.dlma.com/python_pinger.git/blob/master/long_lived_looping_locked_workers.py)**
95) 
96) ### Upsides
97) 
98) Got rid of the entire printer thread.
99) 
100) ### Downsides
101) 
102) Uses a lock, which in more complex applications with multiple locks becomes difficult to reason about.
103)