Laxmikant Laxmikant - 16 days ago 8
Python Question

How to use cpulimit to a python script

From the answer to my old question, I gone through this and some more from google. But I did not understand, how can I

cpulimit
for a python script.
Followings are my try:

cpulimit --limit 20 --exe ./read_heavy_csv.py

cpulimit --limit 20 --exe "python read_heavy_csv.py"

cpulimit -P ./read_heavy_csv.py -l 20


and some more...

But in each case getting warning :

Warning: no target process found. Waiting for it...


I could not find any documentation to explain running a python script with
cpulimit
. I also gone through these too but did not get any clue

Answer

Instead of executing a program, cpulimit limits the cpu usage of running process. You need to run the program separately.

Run the program normally:

python read_heavy_csv.py

Open other terminal, and run the following command (pgrep ... will give you the pid of the previous command)

cpulimit --limit=10 -p `pgrep -f read_heavy_csv.py`