Skip to content

Instantly share code, notes, and snippets.

@lhchavez
Created May 6, 2015 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lhchavez/7552d955150a54000906 to your computer and use it in GitHub Desktop.
Save lhchavez/7552d955150a54000906 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# Invoke n instances of sudo in parallel. This works fine in all machines I've
# tried this on, except Windows Azure VMs, where occasionally invocations to
# sudo take ~5s to complete when n > 1.
# Tested on a Standard_A1 instance w/Ubuntu Utopic, kernel 3.16.0-34-generic.
import subprocess
import sys
import time
sudo_args = ['/usr/bin/sudo', '/bin/true']
def run(n):
t0 = time.time()
processes = [subprocess.Popen(sudo_args) for i in xrange(n)]
pids = [p.pid for p in processes]
assert(all([p.wait() == 0 for p in processes]))
t1 = time.time()
delta = (t1 - t0) * 1000
if delta >= 1000:
print '%.2f %.2f %.2f %r' % (t0, t1, delta, pids)
if __name__ == '__main__':
try:
n = int(sys.argv[1])
except:
sys.stderr.write('Usage: python %s <processes>\n' % sys.argv[0])
sys.exit(1)
while True:
run(n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment