Advertisement
DeaD_EyE

playing around with signals 2

Mar 31st, 2021
1,116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import time
  2. import sys
  3. import signal
  4. from subprocess import DEVNULL, PIPE, Popen
  5. from functools import partial
  6.  
  7.  
  8. signal_value = int(sys.argv[1])
  9. signal_value = signal.Signals(signal_value)
  10. print("Using signal", signal_value)
  11.  
  12. cmd = ["python", "guard.py", signal_value.name]
  13. make_proc = partial(Popen, cmd)
  14. options = dict(stdout=DEVNULL, stderr=DEVNULL)
  15.  
  16.  
  17. start = time.monotonic()
  18. procs = [make_proc(**options) for _ in range(4)]
  19.  
  20. print("Waiting to finish")
  21. returncodes = set()
  22. for proc in procs:
  23.     returncodes.add(proc.wait())
  24.  
  25. if len(returncodes) == 1 and returncodes.pop() == signal_value:
  26.     print("All return codes are", signal_value)
  27. else:
  28.     print("Something went wrong.")
  29.     print("Got following return codes", returncodes)
  30.  
  31.  
  32. end = time.monotonic()
  33.  
  34. print(f"Starting, sending signal, waiting to finish took {end-start:.2f} seconds.")
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement