Guest User

Untitled

a guest
May 26th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. import time
  2. import shlex
  3. from subprocess import *`
  4.  
  5. #list of commands
  6. commands = ['sleep 5', 'ls -l', 'find /usr','sleep 3','uptime']
  7.  
  8. #executing each command in the list
  9. for command in commands:
  10. cmd = shlex.split(command)
  11. # Need to time this and the moment it is finished executing.
  12. x = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
  13.  
  14. import threading,time,subprocess,shlex
  15.  
  16. time_dict = {}
  17.  
  18. def time_me(command):
  19. start_time = time.time()
  20. cmd = shlex.split(command)
  21. subprocess.call(cmd)
  22. time_dict[command] = time.time() - start_time
  23.  
  24. threads = []
  25. commands = ['sleep 5', 'ls -l', 'find /usr','sleep 3','uptime']
  26.  
  27. for command in commands:
  28. t = threading.Thread(target=time_me,args=(command,))
  29. t.start()
  30. threads.append(t)
  31.  
  32. for t in threads:
  33. t.join()
  34.  
  35. print(time_dict)
  36.  
  37. import time
  38. import shlex
  39. from subprocess import *
  40.  
  41. #list of commands
  42. commands = ['sleep 5', 'ls -l', 'sleep 3', 'uptime'] # 'find /usr' made me really impatient
  43. times = {}
  44. running = {}
  45. #executing each command in the list
  46. for command in commands:
  47. start_time = time.time()
  48. cmd = shlex.split(command)
  49. times[command] = time.time()
  50. running[command] = Popen(cmd,
  51. # no shell=True, shlex already did the split
  52. stdout=PIPE, stderr=PIPE)
  53.  
  54. while len(running):
  55. finished = set()
  56. for cmd, proc in running.items():
  57. if proc.poll() is not None:
  58. times[cmd] = time.time() - times[cmd]
  59. finished.add(cmd)
  60.  
  61. for cmd in finished:
  62. del running[cmd]
  63.  
  64. print(times)
Add Comment
Please, Sign In to add comment