Newbie getting desperate with for

Werner wdahn at netfront.net
Thu Feb 17 18:10:28 EST 2011


On 18/02/11 02:40, Alister Ware wrote:
> On Thu, 17 Feb 2011 16:42:05 +0800, Werner wrote:
> 
>> On 17/02/11 16:39, Chris Rebert wrote:
>>> On Thu, Feb 17, 2011 at 12:27 AM, Werner <wdahn at netfront.net> wrote:
>>>> I have a trivially simple piece of code called timewaster.py:
>>>> ____________________________________________________
>>>>
>>>> while True:
>>>>    i = 0
>>>>    for i in range(10):
>>>>        break
>>>> _____________________________________________________
>>>>
>>>> It runs fine with Eric but when I try to run it from shell...
>>>>> ./timewaster.py
>>>> ./timewaster.py: line 4: syntax error near unexpected token `('
>>>> ./timewaster.py: line 4: `    for i in range(10):'
>>>>
>>>> I've tried this on openSuse 11.3 and Kubuntu 10.04, both use Python
>>>> version 2.6.5, both show the above.
>>>>
>>>> Before I tear out my hair any more (only 3 left) I thought I'd ask
>>>> here what I am doing wrong.
>>>
>>> Looks like it's being run as a shell script rather than through the
>>> Python interpreter (hence why the error is not in the form of an
>>> exception with a traceback).
>>>
>>> Try adding:
>>>
>>> #!/usr/bin/env python
>>>
>>> as the first line in your file. This tells the shell to run the script
>>> using Python.
>>>
>>> Or alternatively, instead of:
>>>
>>> ./timewaster.py
>>>
>>> use:
>>>
>>> python timewaster.py
>>>
>>> which likewise explicitly invokes the Python interpreter.
>>>
>>> Cheers,
>>> Chris
>>> --
>>> http://blog.rebertia.com
>>
>> Yes, that was it. AYAA!
>>
>> Thank you very much.
> 
> may I ask what is the purpose of this code segment, it does not look like 
> it would achieve much?
> 
> 
> 
>  
It is meant to put load on a CPU, RAM and disk (swap). The code now
looks like this:
#!/usr/bin/python
while True:
    i = 0
    for i in range(20000000):
        break

I needed this to get an idea how virtual machines inside a host perform
when you run a few instances of it.

The objective was at what point responsiveness gets severely degraded by
load. To measure that I used
__________________________________
#!/usr/bin/python
import time

def time_it(threshold , period):
    t0 = time.time()    # daytime at start of period
    time.sleep(period)
    t1 = time.time()    # daytime at end of period
    t2 = t1-t0          # calculate actual lenght of period
    # subtract how long it should have been
    t3 =(t2 - period) *1000
    if t3>=threshold:   # if the differnce is too high report it
        print time.strftime('%Y/%m/%d %H:%M:%S   ') , round(t3, 2), "ms"


while True:
    time_it(5.0, 0.1)
___________________________________

Not very sophisticated but it gave me a few clues of what I'm interested in.

Regards
Werner Dahn




More information about the Python-list mailing list