Calculate the optimal number of parallel processes adaptively based on # CPU cores and load
()
| 51 | |
| 52 | |
| 53 | def get_optimal_workers(): |
| 54 | """Calculate the optimal number of parallel processes adaptively based on # CPU cores and load""" |
| 55 | try: |
| 56 | cpu_count = multiprocessing.cpu_count() |
| 57 | except NotImplementedError: |
| 58 | cpu_count = 6 # default |
| 59 | |
| 60 | # Manim rendering is CPU-intensive; usually set workers to CPU cores or cores minus one |
| 61 | # reserve 1 core for system/other processes |
| 62 | optimal = max(1, cpu_count - 1) |
| 63 | |
| 64 | # If the machine is high-performance multicore (>16 cores), |
| 65 | # it's appropriate to limit the number of workers to avoid memory overflow |
| 66 | if optimal > 16: |
| 67 | optimal = 16 |
| 68 | |
| 69 | print(f"⚙️ Detected {cpu_count} cores, using {optimal} parallel processes") |
| 70 | return optimal |
| 71 | |
| 72 | |
| 73 | def monitor_system_resources(): |