import multiprocessing import time import math def cpu_intensive_task(): # CPU-intensive task with more complex calculations counter = 0 while True: math.factorial(1000) # Calculation of factorial which is CPU-intensive 2 ** (counter % 10000) # Exponentiation to avoid overflow by resetting occasionally counter += 1 def main(): # Number of CPU cores num_cores = multiprocessing.cpu_count() print(f"Creating {num_cores} processes to maximize CPU usage.") # Create a process for each core processes = [] for _ in range(num_cores): p = multiprocessing.Process(target=cpu_intensive_task) p.start() processes.append(p) # Allow processes to run for some time try: while True: time.sleep(10) # Adjust time to keep the processes running except KeyboardInterrupt: print("Stopping CPU-intensive tasks.") for p in processes: p.terminate() # Terminate each process p.join() # Wait for processes to properly shut down if __name__ == "__main__": main()