简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM