cpu usage limit

M

mmf

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
 
R

rbt

mf said:
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)
 
G

garabik-news-2005-05

rbt said:
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....



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

rbt

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

Markus Franz

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
 
G

Grant Edwards

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

Paul Rubin

mmf said:
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.
 
P

Peter Hansen

rbt said:
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).

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
 
E

elbertlev

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

garabik-news-2005-05

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top