Last active
August 29, 2015 14:02
-
-
Save kfei/63212deff76d6e1ca2a1 to your computer and use it in GitHub Desktop.
Python: virsh-batch-start-vm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import threading | |
import Queue | |
import commands | |
import time | |
# thread class to run a command | |
class ExampleThread(threading.Thread): | |
def __init__(self, cmd, queue): | |
threading.Thread.__init__(self) | |
self.cmd = cmd | |
self.queue = queue | |
def run(self): | |
# execute the command, queue the result | |
(status, output) = commands.getstatusoutput(self.cmd) | |
self.queue.put((self.cmd, output, status)) | |
# queue where results are placed | |
result_queue = Queue.Queue() | |
cmds = [] | |
for i in range(int(sys.argv[1]), int(sys.argv[2]) + 1): | |
cmd = "virsh start %s" % ('win7-' + str(i)) | |
cmds.append(cmd) | |
for cmd in cmds: | |
thread = ExampleThread(cmd, result_queue) | |
thread.start() | |
# print results as we get them | |
while threading.active_count() > 1 or not result_queue.empty(): | |
while not result_queue.empty(): | |
(cmd, output, status) = result_queue.get() | |
print('%s:' % cmd) | |
print(output) | |
print('='*60) | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment