With the era of multi-core CPU, parallel, concurrency or multiprocessing is quite possible now. Below is an example of a Python code that shows how to use multiprocessing module to run multiple commands. import multiprocessing import subprocess # Shell command/s Function def multi_run_command(command): try: result = subprocess.run(command, shell=True, capture_output=True, text=True) return f"Command: {command}\nOutput:\n{result.stdout}" except Exception as e: return f"Command: {command}\nError: {e}" if __name__ == "__main__": # List of dig commands to run / Replace the commands as desired commands = ["dig @1.1.1.1 google.com", "dig @8.8.4.4 yahoo.com", "dig @9.9.9.9 bing.com"] # Create and specify limit of pool worker processes with multiprocessing.Pool(processes=3) as pool: #limit to 3 cores or workers results = pool.map(multi_run_command, commands) #use the m...
Quick Bytes IT Stuff
Make the world a better place by sharing knowledge, ideas and anything that you can give that comes from the heart.