Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import shlex
- from subprocess import *`
- #list of commands
- commands = ['sleep 5', 'ls -l', 'find /usr','sleep 3','uptime']
- #executing each command in the list
- for command in commands:
- cmd = shlex.split(command)
- # Need to time this and the moment it is finished executing.
- x = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
- import threading,time,subprocess,shlex
- time_dict = {}
- def time_me(command):
- start_time = time.time()
- cmd = shlex.split(command)
- subprocess.call(cmd)
- time_dict[command] = time.time() - start_time
- threads = []
- commands = ['sleep 5', 'ls -l', 'find /usr','sleep 3','uptime']
- for command in commands:
- t = threading.Thread(target=time_me,args=(command,))
- t.start()
- threads.append(t)
- for t in threads:
- t.join()
- print(time_dict)
- import time
- import shlex
- from subprocess import *
- #list of commands
- commands = ['sleep 5', 'ls -l', 'sleep 3', 'uptime'] # 'find /usr' made me really impatient
- times = {}
- running = {}
- #executing each command in the list
- for command in commands:
- start_time = time.time()
- cmd = shlex.split(command)
- times[command] = time.time()
- running[command] = Popen(cmd,
- # no shell=True, shlex already did the split
- stdout=PIPE, stderr=PIPE)
- while len(running):
- finished = set()
- for cmd, proc in running.items():
- if proc.poll() is not None:
- times[cmd] = time.time() - times[cmd]
- finished.add(cmd)
- for cmd in finished:
- del running[cmd]
- print(times)
Add Comment
Please, Sign In to add comment