简体   繁体   中英

How do I reduce CPU and memory usage by a python program?

I have a python program that uses a lot of my CPU's resources. While it is fine on my regular PC, I'm afraid it might be too much to handle for my Raspberry Pi. Speed is not an issue. I don't care if my code is executed slowly as I am implementing a real time system that executes the code only once every few hours, but my CPU needs to be freed up as I would also be running other processes simultaneously. Is there anyway I can reduce the resources that it takes from the CPU at the cost of speed of execution? Any help would be appreciated, thank you

While you sure can tinker with your program and make it more optimized, the fact is that all programs are generally designed to take as much CPU as they need in order to finish in smallest time possible.

I see two ways to achieve your goal:

  1. Raspberry pi is Linux right? So just lower process priority of the python interpreter running your script. this would make sure that other programs can have CPU if they need it

  2. In your script, sleep for few milliseconds every few milliseconds.. ugly, but could do the trick

But option one is probably way to go.

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