hitochan
hitochan

Reputation: 1068

Change the number of processes to spawn depending on the CPU usage in Python

I want to change the number of processes to spawn depending on the CPU usage in python. Suppose the CPU in my laptop comes with 8 cores and currently 3 cores are fully used, which means maximum of 5 cores are available. Then I want my python program to spawn 5 processes on runtime. Is there any way to achieve this in Python?

Upvotes: 2

Views: 352

Answers (1)

nohup
nohup

Reputation: 3165

You can get the number of cores using

import multiprocessing
cores = multiprocessing.cpu_count()

and to get the current load average,

import os
loadavg = os.getloadavg()[0]

You can use these to determine your number of spawned processes.

Upvotes: 7

Related Questions