[Tutor] continuous running of a method

Greg Bair gregbair at gmail.com
Wed Aug 25 07:56:14 CEST 2010


On 08/25/2010 01:25 AM, Nitin Das wrote:
> The problem with this while loop is if your random value doesn't lie 
> between the mentioned range then ur 100% cpu would be utilized. The 
> one thing u can do is to sleep for some time lets say 0.5 sec after 
> every while loop iteration , in this case ur cpu utilization will go down.
It's not that the value doesn't lie between the mentioned range.

What I'm doing is randomly fetching an item from a list of dicts 
(multi-dimensional ones from a JSON response) and accessing a value from 
it, but not every item has the key I'm looking for (can't change that).  
I suppose what I could do to not randomize every time is to iterate 
through the list and create a new list that only contains dicts that 
have that key, then get a random one from that.  I suppose that would be 
more efficient.

Any thoughts?

Greg
>
> --nitin
>
> On Mon, Aug 23, 2010 at 8:21 PM, bob gailer <bgailer at gmail.com 
> <mailto:bgailer at gmail.com>> wrote:
>
>      On 8/23/2010 1:00 AM, Greg Bair wrote:
>
>         I have a method (I'll call it foo) that will either return
>         None or an object depending on a random value generated.  What
>         I want to happen is that if I call foo(), i.e, f = foo() and
>         it returns None, to re-call it until it returns something
>         else.  I would think this would be done with a while loop, but
>         can't seem to get the details right.
>
>
>     Even though a while will work, you will have tied up the CPU until
>     the loop terminates. This is never a good idea.
>
>     What is your higher level goal?
>
>



More information about the Tutor mailing list