cpu usage limit

rbt rbt at athop1.ath.vt.edu
Fri May 27 11:49:43 EDT 2005


garabik-news-2005-05 at kassiopeia.juls.savba.sk wrote:
> 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:

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



More information about the Python-list mailing list