import os
import gc
import time
import multiprocessing as mp
import numpy as np
N_WORKERS = 8
def cpu_stress(duration=5):
print(f"[{os.getpid()}] CPU stress started.")
end = time.time() + duration
while time.time() < end:
_ = sum(i * i for i in range(10000)) print(f"[{os.getpid()}] CPU stress done.")
def memory_stress(size_mb=500, duration=3):
print(f"[{os.getpid()}] Memory stress started. Allocating {size_mb}MB.")
arr = np.ones((size_mb * 1024 * 1024 // 8,), dtype="float64") time.sleep(duration)
print(f"[{os.getpid()}] Memory stress done.")
del arr
gc.collect()
def worker_task(index):
print(f"[{os.getpid()}] Worker {index} started.")
cpu_stress()
memory_stress()
print(f"[{os.getpid()}] Worker {index} done.")
def main():
print(f"[{os.getpid()}] Main process starting subprocesses.")
workers = []
for i in range(N_WORKERS): p = mp.Process(target=worker_task, args=(i,))
p.start()
workers.append(p)
for p in workers:
p.join()
print(f"[{os.getpid()}] Main process done.")
if __name__ == "__main__":
main()