Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Run python function each minute

  1. #1
    Join Date
    Mar 2006
    Location
    Denmark - Copenhagen
    Beans
    2,165

    Run python function each minute

    Hey mates,

    I'm trying to run my small python program each minute.
    But it seams to give me some kind of memory leak doing it this way:


    PHP Code:
    def start():
        while 
    True:
                
    Check(hosts)
                
    time.sleep(60)

    start() 
    Check() is the one I want to run every 60 secs. Any ideas?

  2. #2
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Run python function each minute

    Run it in a cron job?
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Mar 2006
    Location
    Denmark - Copenhagen
    Beans
    2,165

    Re: Run python function each minute

    Thanks for your suggestion Bachstelze.
    I thought about that, but running it without cron would be nice.

    Is there any better ways doing this then my method?

  4. #4
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Run python function each minute

    That's how I would do it, then, what makes you think it memory leaks?
    「明後日の夕方には帰ってるからね。」


  5. #5
    Join Date
    Mar 2006
    Location
    Denmark - Copenhagen
    Beans
    2,165

    Re: Run python function each minute

    Quote Originally Posted by Bachstelze View Post
    That's how I would do it, then, what makes you think it memory leaks?
    Because memory consumption keeps going up
    Then it must be my check() function that memory leaks..

    Okay this is how it looks:

    PHP Code:
    import urllib
    import re
    import time

    hosts 
    = [
        
    'www.google.com'
        
    'www.Fail22222.com' # Fails 
        
    ]


    def Check(hosts):
        for 
    hosts in hosts:
            try:
                
    urllib.urlopen("http://"+hosts).read()
                
    find re.findall("word" ,f)
                 
    find2 re.findall("DOCTYPE" ,f)
                print 
    hosts
                
    try:
                if 
    find[0] == "word":
                     print 
    "Found word"       
                
    genPHPuP(hosts)
                
    except IndexError:
                try:
                    if 
    find2[0] == "DOCTYPE":
                    print 
    "Found DOCTYPE"
                    
    genPHPuP(hosts)
                
    except IndexError:
                        print 
    "Page does not contain DOCTYPE or word"
                    
    genPHPwarn(hosts)
            
    except IOError:
                print 
    hosts
                
    print "Page does not contain DOCTYPE or word! Server is down!"
                
    genPHPdown(hosts)



    def genPHPuP(hosts):
           
        try:
            
    filename hosts+".php"
            
    print "PHPuP: Writing file: %s" filename
            file 
    open(filename'w')
            
    file.write("<img src=pics/greenV.jpg>" )
            
    file.close()
        
    except IOError:
            print 
    "PHPup: Couldn't write to file %s" filename



    def genPHPdown
    (hosts):

        try:
            
    filename hosts+".php"
            
    print "PHPdown: Writing file: %s" filename
            file 
    open(filename'w')
            
    file.write("<img src=pics/redX.png>" )
            
    file.close()
        
    except IOError:
            print 
    "PHPdown: Couldn't write to file %s" filename



    def genPHPwarn
    (hosts):

        try:
            
    filename hosts+".php"
            
    print "PHPWarn: Writing file: %s" filename
            file 
    open(filename'w')
            
    file.write("<img src=pics/warn.png>" )
            
    file.close()
        
    except IOError:
            print 
    "PHPWarn: Couldn't write to file %s" filename



    # Starts Check() every 60 sec
    def start():
        while 
    True:
                
    Check(hosts)
                
    time.sleep(60)

    start() 
    What I am trying to do is to go through the lists of hosts, check them for 2 different words, and write to a file if it finds the word, does not find the word or if the server is down.

    Can you spot any leaks in that? I'm still learning Python.

    Thanks a lot!
    Last edited by aktiwers; July 18th, 2010 at 03:55 PM.

  6. #6
    Join Date
    Mar 2006
    Location
    Denmark - Copenhagen
    Beans
    2,165

    Re: Run python function each minute

    No one?

  7. #7
    Join Date
    Aug 2007
    Beans
    949

    Re: Run python function each minute

    Python manages its own memory. Memory usage may go up, but this is a consequence of you not having control over how Python decides to allocate/deallocate memory for things, the point being that you shouldn't worry about it.

    EDIT:

    But as a manner of style, I wouldn't do this:

    PHP Code:
    for hosts in hosts
    While it works, it's confusing. My personal idiom in these situations is to do this:

    PHP Code:
    for host in hosts
    Which (at least to me) is clearer.
    Last edited by soltanis; July 18th, 2010 at 11:35 PM.

  8. #8
    Join Date
    Mar 2006
    Location
    Denmark - Copenhagen
    Beans
    2,165

    Re: Run python function each minute

    Thanks a lot Soltanis!

    I will fix that

    Actually after letting the script run all day, memory usage seams to increase slower and slower, but still it is growing. after running for about 8 hours it was taking up 10,7mb.

    Do you think it would stall at some point or just keep going up? Since my plan is to have the script running 24/7.
    Last edited by aktiwers; July 18th, 2010 at 11:46 PM.

  9. #9
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Run python function each minute

    PHP Code:
    urllib.urlopen("http://"+hosts).read() 
    Do you close that file-like object, somewhere? I don't know if this is the responsible for the memory consumption, but you should consider it, nonetheless.

    PHP Code:
    def genPHPuP(hosts):
        
    pass

    def genPHPdown
    (hosts):
        
    pass

    def genPHPwarn
    (hosts):
        
    pass 
    This three functions are exactly the same, except for a few variable elements, I suggest merging them in one. (For readability)

    PHP Code:
    print "Page does not contain DOCTYPE or word! Server is down!" 
    Have you thought on just "pinging" the server?

    Do you think it would stall at some point or just keep going up? Since my plan is to have the script running 24/7.
    That's an undecidable problem. But I would assume that it will grow indefinitely (as there are no clues that suggest otherwise). Try switching to python3 or newer, in case it's an old bug.
    Last edited by Can+~; July 18th, 2010 at 11:58 PM.
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  10. #10
    Join Date
    Mar 2006
    Location
    Denmark - Copenhagen
    Beans
    2,165

    Re: Run python function each minute

    I really appreciate your inputs! Thanks alot.

    The reason I did not choose to ping the sites, is that pinging will only show if the server is online, but not if the specific page is up.

    I actually started out trying to write those together in one function, but my newbieness made me lazy! In the end I want to rewrite the whole thing and somehow make it check if the file it is writing to, already contains the info it is writing, and only write when the file need to be updated. Right now it writes everytime.

    And you are perfectly correct, I don't close that thing anywhere.. Let me try that and see what happends.

Page 1 of 2 12 LastLast

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
  •