How can I make sure that a Python process does not use more that 30% of
the CPU at any time. I only want that the process never uses more, but
I don't want the process being killed when it reaches the limit (like
it can be done with resource module).
Can you help me?
Thanks in advance.
Are you looping during a cpu intensive task? If so, make it sleep a bit
for x in cpu_task:
or like this (untested!)
finished = False
while not finished:
before = time.time()
do(x) # sets finished if all was computed
after = time.time()
delta = after-before
now the trick: do(x) can be a single piece of code, with strategically placed yield's
| Radovan Garabík http://kassiopeia.juls.savba.sk/~garabik/ |
| __..--^^^--..__ garabik @ kassiopeia.juls.savba.sk |
Antivirus alert: file .signature infected by signature virus.
Hi! I'm a signature virus! Copy me into your signature file to help me spread!
No, I don't use an intensive loop. I have about 1200 lines of code
inside a process - is there nothing like
Why don't you just write 'while True'??? 'while not false' is like
saying 'I am not unemployed by Microsoft' instead of saying 'I am
employed by Microsoft'. It's confusing, complex and unnecessary. Lawyers
call it circumlocution (talking around the truth).
>>> How can I make sure that a Python process does not use more that 30% of
>>> the CPU at any time. I only want that the process never uses more, but
>>> I don't want the process being killed when it reaches the limit (like
>>> it can be done with resource module).
>> Are you looping during a cpu intensive task? If so, make it sleep a bit
>> like this:
>> for x in cpu_task:
> or like this (untested!)
> finished = False
> while not finished:
> before = time.time()
> do(x) # sets finished if all was computed
> after = time.time()
> delta = after-before
> now the trick: do(x) can be a single piece of code, with
> strategically placed yield's all over....
Since you have no way of knowing that your process was the only
one running between the two calls to time.time(), you're
placing an upper bound on how much CPU time you're using, but
the actual usage is unknown and may be much lower on a heavily
Running for 100ms and sleeping for 333ms results in an upper
limit of 25% rather than 30%. Sleeping for (delta * 7.0/3.0)
gives a 30% upper bound.
All that aside, it seems to me that this situation is analogous
to when people waste all sorts of effort trying to write clever
applications that cache parts of files or other data structures
in main memory with backing store on disk. They end up with a
big, complicated, buggy app that's slower and requires more
resources than a far simpler app that lets the OS worry about
IOW, you're probably better off not trying to write application
code that tries to out-think your OS. Use whatever prioritizing
scheme your OS kernel provides for setting up a low priority
"background" task, and let _it_ worry about divvying up the CPU.
That's what it's there for, and it's got a far better picture
of resource availability and demand.
Grant Edwards grante Yow! Yow! It's a hole
at all the way to downtown
In general you can only do that with a real-time operating system.
Most other OS's will let you adjust process priorities so that you can
prevent your Python process from hogging cycles away from other
processes. But if the machine is idle and nobody else wants the
cycles, Python will get all of them.
The answer to your question "why not write 'while True'?" is to be found
in the helpful comment he put on the line with "do(x)".... Note that
"finished" is a flag, so "sets finished" sort of explains the whole thing.
Why do you want to restrict CPU usage to 30%? In Windows I run CPU
intesive therads on IDLE priority, while interfacand/or communication
threads run on normal. This gives me best of two worlds:
1. I use 100% CPU (good) and
2. progam i responcive (very good).
There is no cross platform way to change the thread priority. But most
OS (as well as thread libraries) support setting priorities.
there might be three reasons:
1) less power consumed (notebooks, PDA's)
2) less heat from CPU
3) (cross platform) scheduling of low priority tasks (e.g. all my
background tasks are already running with lowest priority since I do not
want them to influence my desktop in any way, but still I want some of them to be
of higher priority)
generally, modern OS'es do not provide any ways to schedule tasks with
such constrains, which makes the question perfectly legitimate