import subprocess, os, signal, psutil from math import ceil from subprocess import PIPE def kill_proc_tree(pid, including_parent=True): parent = psutil.Process(pid) children = parent.children(recursive=True) for child in children: child.kill() gone, still_alive = psutil.wait_procs(children, timeout=5) if including_parent: parent.kill() parent.wait(5) def create_procs(): typ = input('Video (v) or playlist (p) or threaded playlist (t)? ') Id = input('Id: ') args = input('Additional arguments for downloader: ') proc = [] if typ == 't': threads = int(input('Threads: ') or 1) total = int(input('Total items: ')) div = ceil(total/threads) a = 1 while a+div < total: b = a+div-1 print(f'{a}-{b}') proc.append(subprocess.Popen(f'py main.py p {Id} "--playlist-items {a}-{b} {args}"', creationflags=subprocess.CREATE_NEW_CONSOLE)) a = b + 1 print(f'{a}-{total}') proc.append(subprocess.Popen(f'py main.py p {Id} "--playlist-items {a}-{total} {args}"', creationflags=subprocess.CREATE_NEW_CONSOLE)) else: proc.append(subprocess.Popen(f'py main.py {typ} {Id} {args}', creationflags=subprocess.CREATE_NEW_CONSOLE)) return proc print(''' #################################### # # # COMMENT AWARDS # # MEME EXTRACTOR # # # # PROGRAMMED BY BRENDAN WESTLEY # # # # (FOR CLI, USE PROGRAM "main.py") # #################################### ''') while True: cmd = input('Command: ') if cmd == 'help': print('''Help: new - start new process stop - stop all running processes quit - stop all running processes and exit''') elif cmd == 'new': try: create_procs() except Exception as e: print(f'ERROR: {e}') elif cmd == 'stop': me = os.getpid() kill_proc_tree(me, False) elif cmd == 'quit': me = os.getpid() kill_proc_tree(me)