Results 1 to 10 of 10

Thread: python script eating processor

  1. #1
    Join Date
    Jan 2012
    Beans
    161

    python script eating processor

    Code:
    import osimport timedef getWeather(interval):	a = int(time.time()) + int(interval)	b = int(time.time())	dataFile = open('/sdcard/weather.txt','a')	while b < a:		b = int(time.time())		continue	b = '-' * 50	c = '\n'	dataFile.write(b)	os.popen2('metar -d KILG >> /sdcard/weather.txt')	dataFile.write(b)	dataFile.write(c)	a = 0def initiateSequence():	print 'Time between readings?'	elapse = raw_input('>')	while True:		getWeather(elapse)if __name__ == '__main__':	initiateSequence()
    when i run this as a program from the bash shell, my processor usage is 100% on that program. Any way to cut it down?

  2. #2
    Join Date
    Jan 2012
    Beans
    161

    Re: python script eating processor

    how come my code is not posting vertically?

  3. #3

    Re: python script eating processor

    It almost certainly has to do with how you're copying it. It might be your editor. Without more information that's as much as I can say. With that in mind, this is exactly what the preview button is for -- so you can see how your post will look before submitting it. Proofread, and if you're having a technical problem posting code make a new thread about that.

  4. #4
    Join Date
    Apr 2013
    Location
    43.49°N 7.46°E
    Beans
    117
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: python script eating processor

    this should be the code
    Code:
    import os
    import time
    
    def getWeather(interval):
        a = int(time.time()) + int(interval)
        b = int(time.time())
        dataFile = open('/sdcard/weather.txt','a')
        while b < a:
            b = int(time.time())
            continue
        b = '-' * 50
        c = '\n'
        dataFile.write(b)
        os.popen2('metar -d KILG >> /sdcard/weather.txt')
        dataFile.write(b)
        dataFile.write(c)
        a = 0
    
    def initiateSequence():
        print 'Time between readings?'
        elapse = raw_input('>')
        while True:
            getWeather(elapse)
    
    if _name__ == '__main__':
        initiateSequence()

  5. #5
    prodigy_ is offline May the Ubuntu Be With You!
    Join Date
    Mar 2008
    Beans
    1,219

    Re: python script eating processor

    Any particular reason for using python over bash? I don't see anything here that bash couldn't accomplish.

    Anyway
    Code:
    while True:
    is what eats CPU. You need to add time.sleep(<number>) somewhere.
    Last edited by prodigy_; July 15th, 2013 at 08:55 AM.

  6. #6
    Join Date
    Apr 2013
    Location
    43.49°N 7.46°E
    Beans
    117
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: python script eating processor

    Quote Originally Posted by wingnut2626 View Post
    how come my code is not posting vertically?
    you might use ideone, an online editor for a lot of programming languages.
    It allows you to test immediately your code.
    That said, why do you use os.popen instead of subprocess?

  7. #7
    Join Date
    Apr 2013
    Location
    43.49°N 7.46°E
    Beans
    117
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: python script eating processor

    the following link shows a tip for controlling CPU and RAM usage:
    http://stackoverflow.com/questions/2.../278271#278271

  8. #8
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: python script eating processor

    What kiils your CPU is this:

    Code:
    while b < a:         
       b = int(time.time())         
       continue
    

    In other words you run a tight loop while waiting for the end of the interval. use sleep(interval) instead as suggested above.


  9. #9
    Join Date
    Dec 2007
    Location
    Behind you!!
    Beans
    977
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: python script eating processor

    I won't comment on the CPU usage as others have already covered that, but here is my list of issues in that code
    • non-descriptive variable names
    • pointless variables used once
    • reuse of variable names
    • why reset 'a' back to 0
    • use subprocess instead of popen2
    • using a while loop to wait for a time period means unnecessary calls to time.time - use time.sleep instead
    • you have 3 calls to file.write - If you read in your metar results you could simplify it to 1 write



    computer-howto
    Linux is not windows
    Fluxbox & Flux menu how to
    Programming is an art. Learn it, Live it, Love it!


  10. #10
    Join Date
    Jan 2012
    Beans
    161

    Re: python script eating processor

    Thanks guys for the help

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •