By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,422 Members | 1,282 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,422 IT Pros & Developers. It's quick & easy.

cpu usage limit

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
rbt

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)
Jul 19 '05 #2

P: n/a
rbt <rb*@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:
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!
Jul 19 '05 #3

P: n/a
> 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
Jul 19 '05 #4

P: n/a
rbt
ga******************@kassiopeia.juls.savba.sk wrote:
rbt <rb*@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).
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....

Jul 19 '05 #5

P: n/a
On 2005-05-27, ga******************@kassiopeia.juls.savba.sk <ga******************@kassiopeia.juls.savba.sk> wrote:
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!
Jul 19 '05 #6

P: n/a
"mmf" <mf****@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.
Jul 19 '05 #7

P: n/a
rbt wrote:
ga******************@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
Jul 19 '05 #8

P: n/a
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.

Jul 19 '05 #9

P: n/a
el*******@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

--
-----------------------------------------------------------
| 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!
Jul 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.