Created
February 12, 2016 06:19
-
-
Save sougou/fff5f4c88f9457ae7ff4 to your computer and use it in GitHub Desktop.
This file contains 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/python | |
"""This executes the specified commands from the output of vtlist.py. | |
This tool expects its input to be piped from vtlist.py. Please | |
view the documentation of vtlist.py on how to use it. | |
""" | |
import argparse | |
import subprocess | |
import sys | |
import threading | |
class Getter(threading.Thread): | |
def __init__(self, command, tablet, tablet_id): | |
threading.Thread.__init__(self) | |
self.command = command | |
self.tablet = tablet | |
self.tablet_id = tablet_id | |
def run(self): | |
try: | |
result=subprocess.check_output([self.command, self.tablet_id]) | |
except subprocess.CalledProcessError as e: | |
result = str(e) | |
# Don't use print. It's not thread safe. | |
sys.stdout.write("%s %s\n" % (self.tablet, result.strip())) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument("command", type=str, help="request to send to servers") | |
args = parser.parse_args() | |
threads = [] | |
for line in sys.stdin: | |
line = line.strip() | |
parts = line.split(" ") | |
thread = Getter(args.command, line, parts[0]) | |
thread.start() | |
threads.append(thread) | |
for thread in threads: | |
thread.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment