cpu usage limit

17 views
Skip to first unread message

mmf

unread,
May 27, 2005, 11:03:36 AM5/27/05
to
Hi.

My problem:
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.

Best regards,
Markus

rbt

unread,
May 27, 2005, 11:07:56 AM5/27/05
to

Are you looping during a cpu intensive task? If so, make it sleep a bit
like this:

for x in cpu_task:
time.sleep(0.5)
do(x)

garabik-ne...@kassiopeia.juls.savba.sk

unread,
May 27, 2005, 11:21:11 AM5/27/05
to

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
time.sleep(delta*10/3.)

now the trick: do(x) can be a single piece of code, with strategically placed yield's
all over....

--
-----------------------------------------------------------
| 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!

Markus Franz

unread,
May 27, 2005, 11:54:15 AM5/27/05
to
> Are you looping during a cpu intensive task? If so, make it sleep a bit
> like this:
>
> for x in cpu_task:
> time.sleep(0.5)
> do(x)

No, I don't use an intensive loop. I have about 1200 lines of code
inside a process - is there nothing like

xyz.setlimit(xyz.cpu, 0.30)

???

Thank.
Markus

rbt

unread,
May 27, 2005, 11:49:43 AM5/27/05
to
garabik-ne...@kassiopeia.juls.savba.sk wrote:
> rbt <r...@athop1.ath.vt.edu> wrote:
>
>>mf wrote:
>>
>>>Hi.
>>>
>>>My problem:
>>>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.
>>>
>>>Best regards,
>>>Markus
>>>
>>
>>Are you looping during a cpu intensive task? If so, make it sleep a bit
>>like this:
>>
>>for x in cpu_task:
>> time.sleep(0.5)
>> do(x)
>
>
> or like this (untested!)
>
> finished = False
> while not finished:

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).

Grant Edwards

unread,
May 27, 2005, 12:07:31 PM5/27/05
to

>>> 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:
>> time.sleep(0.5)
>> do(x)
>
> 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
> time.sleep(delta*10/3.)
>
> 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
loaded machine.

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
memory management.

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
visi.com Burbank!

Paul Rubin

unread,
May 27, 2005, 6:04:39 PM5/27/05
to
"mmf" <mf1...@arcor.de> writes:
> 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?

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.

Peter Hansen

unread,
May 27, 2005, 6:15:49 PM5/27/05
to
rbt wrote:

> garabik-ne...@kassiopeia.juls.savba.sk wrote:
>> finished = False
>> while not finished:
>
> 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).
>
>> before = time.time()
>> do(x) # sets finished if all was computed
>> after = time.time()
>> delta = after-before
>> time.sleep(delta*10/3.)

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.

-Peter

elbe...@hotmail.com

unread,
May 27, 2005, 9:44:12 PM5/27/05
to
I understand, that what I suggest does not solve the problem you want,
but..

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.

garabik-ne...@kassiopeia.juls.savba.sk

unread,
May 30, 2005, 1:54:17 AM5/30/05
to
elbe...@hotmail.com wrote:
> I understand, that what I suggest does not solve the problem you want,
> but..
>
> Why do you want to restrict CPU usage to 30%? In Windows I run CPU

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

Reply all
Reply to author
Forward
0 new messages