Multiple Thread Run CMD
December 1, 2024Less than 1 minute
Multiple Thread for Runing CMD
import os
import sys
sys.path.append(os.path.dirname(__file__))
import argparse
from thread_util import thread_util
import ctypes
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--cmd', type=str)
parser.add_argument('--sleep', default=False, action=argparse.BooleanOptionalAction)
return parser.parse_args()
def download(cmd):
print(cmd)
os.system(cmd)
if __name__ == "__main__":
args = parse_args()
thread_obj = thread_util(40)
with open(args.cmd, 'r') as fr:
cmd = fr.readline()
while len(cmd) > 0:
thread_obj.process(download, (cmd,),cmd)
cmd = fr.readline()
thread_obj.wait()
if args.sleep:
# Call the Windows API to put the computer to sleep
ctypes.windll.powrprof.SetSuspendState(False, True, False)
Multiple Thread Utils
import threading
class thread_util:
def __init__(self, thread_max = 20):
self.__thread_pool__ = list()
self.__thread_max__ = 1
if thread_max:
self.__thread_max__ = thread_max
def process(self, target, args, name):
th = threading.Thread(target=target, args=args, name= name)
self.__thread_pool__.append(th)
th.start()
self.__update_thread__()
def wait(self):
self.__update_thread__(1)
def __update_thread__(self, count = None):
c = self.__thread_max__
if count:
c = count
while len(self.__thread_pool__) >= c:
dead_pool = []
for th in self.__thread_pool__:
if not th.is_alive():
dead_pool.append(th)
for item in dead_pool:
self.__thread_pool__.remove(item)
#time.sleep(0.1)