Skip to content

Instantly share code, notes, and snippets.

@Sh1Yo
Last active May 23, 2020 11:03
Show Gist options
  • Save Sh1Yo/e6b7850a6d911793e49e110ed26f130f to your computer and use it in GitHub Desktop.
Save Sh1Yo/e6b7850a6d911793e49e110ed26f130f to your computer and use it in GitHub Desktop.
Get list of commands and run them in parallel.
import os, sys, time
from concurrent.futures import ThreadPoolExecutor
from threading import Thread
class WorkThread(Thread):
def __init__(self, data):
Thread.__init__(self)
self.data = data
def run(self):
os.system(self.data)
def start(data):
thread = WorkThread(data)
thread.start()
thread.join()
if len(sys.argv) < 2 or sys.argv[1] == "-h" or sys.argv[1] == "--help":
print("python3 script {file} {workers} {sleep}")
exit()
delay = 0
workers = int(sys.argv[2])
if len(sys.argv) > 3:
delay = int(sys.argv[3])
with open(sys.argv[1]) as file:
first_cycle = True
times = 0
data = file.read().split("\n")
with ThreadPoolExecutor(max_workers=workers) as executor:
for d in data:
if first_cycle:
times += 1
time.sleep(delay)
if times > workers:
first_cycle = False
executor.submit(start, d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment