cpu usage limit

garabik-news-2005-05 at kassiopeia.juls.savba.sk garabik-news-2005-05 at kassiopeia.juls.savba.sk
Fri May 27 11:21:11 EDT 2005


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



More information about the Python-list mailing list