Skip to content

Commit

Permalink
Temporary fix for the print stats on exits via signals
Browse files Browse the repository at this point in the history
  • Loading branch information
l4m3rx committed Nov 4, 2016
1 parent 7eb3179 commit efc7fb9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions ChangeLog.creole
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
==== November 4, 2016 ====
----------------
* Renaming MyStats class to avoid confusion
* Making myStats global variable so we can print stats when terminating with signal
==== November 3, 2016 ====
----------------
Expand Down
14 changes: 9 additions & 5 deletions ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ class MStats:
avrgTime = 0
fracLoss = 1.0

# NOT Used globally anymore.
# Used as 'global' variale so we can print
# stats when exiting by signal
myStats = MStats


Expand Down Expand Up @@ -309,6 +310,7 @@ def _dump_stats(myStats):

def _signal_handler(signum, frame):
""" Handle exit via signals """
global myStats
_dump_stats(myStats)
print("\n(Terminated with signal %d)\n" % (signum))
sys.exit(0)
Expand Down Expand Up @@ -337,13 +339,15 @@ def verbose_ping(hostname, timeout=3000, count=3,
>>> exit_code = consume[:-1] # The last yield is the exit code.
>>> sys.exit(exit_code)
"""
signal.signal(signal.SIGINT, _signal_handler) # Handle Ctrl-C
if hasattr(signal, "SIGBREAK"):
# Handle Ctrl-Break e.g. under Windows

global myStats

# Handle Ctrl+C
signal.signal(signal.SIGINT, _signal_handler)
if hasattr(signal, "SIGBREAK"): # Handle Ctrl-Break /Windows/
signal.signal(signal.SIGBREAK, _signal_handler)

myStats = MStats() # Reset the stats

mySeqNumber = 0 # Starting value

try:
Expand Down

0 comments on commit efc7fb9

Please sign in to comment.