Is it possible to force your computer to devote less CPU in exchange for more time when running a python program? [duplicate]

StackOverflow https://stackoverflow.com/questions/22109120

Question

I'm running a python program that's a fairly intensive test of many possible scenarios using a big-O of n algorithm. It's just brute-forcing it by testing over a billion different possibilities using at least five nested loops.

Anyway, I'm not concerned with how much time the program takes. It's fine to run in the background for long periods of time, it's just that I can't have it clogging up the CPU. Is there any way in Python (3.3) to devote less CPU to a program in exchange for giving it more time?

Thanks in advance.

Was it helpful?

Solution

A simpler solution might be to nice or re-nice your app. If you are running a Unix variant (Linux, *BSD, Mac), try something like this:

nice -n 19 python my_code.py

More info here: https://en.wikipedia.org/wiki/Nice_(Unix)

On Windows, you can try to set the priority via Task Manager (locate the Python process in Processes tab, right-click and select Set Priority).

OTHER TIPS

First recommendation is the simpler lower than process priority to absolute minimum.

If still not reponsive, you could sprinkle in sleep() calls from from time module to surrender CPU

Or buy a new computer with 4 cores and just let it run. I do this all the time -- works great.

ADDED

Adding time.sleep() calls will leave a single cpu system running "bursty". Also sleep(0) may be effective in an inner loop as is will yield the cpu, but get rescheduled quickly if nothing else wants to use the cpu. OOPS, forgot to check, you are using Linux -- sleep(0) does nothing. You can call the native sched_yield() API, don't think it is built into Python anywhere.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top