Make a function call itself after set amount of time

Discussion in 'Python' started by Bart Nessux, Jan 13, 2004.

  1. Bart Nessux

    Bart Nessux Guest

    How do I make a function call itself every 24 hours. Also, is there a
    way to start the program automatically w/o depending on the OS functions
    like 'Task Scheduler' or 'Start Up Items'... this is on Windows 2k and
    xp. Below is an example of what I'm trying to do.

    TIA

    def ipconfig_email():
    from email.MIMEText import MIMEText
    import smtplib
    import time
    import os

    u = "user name" #Change This to user's name.
    f = "my-email-addy"
    t = "my-email-addy"

    fp0 = os.popen("ipconfig /all", "r")
    fp1 = os.popen("psinfo -d -s", "rb")
    msg = MIMEText(fp0.read() + fp1.read())
    fp0.close()
    fp1.close()

    msg["Subject"] = "%s's IPconfig Report" % u
    msg["From"] = f
    msg["To"] = t

    h = "my.smtp.server"
    s = smtplib.SMTP(h)
    s.sendmail(f, t, msg.as_string())
    s.quit()
    time.sleep(86400) #24 hour sleep
    HOW_DO_I_CALL_THE_FUNCTION_AGAIN?

    ipconfig_email()
    Bart Nessux, Jan 13, 2004
    #1
    1. Advertising

  2. Bart Nessux

    Bart Nessux Guest

    Bart Nessux wrote:
    > How do I make a function call itself every 24 hours. Also, is there a
    > way to start the program automatically w/o depending on the OS functions
    > like 'Task Scheduler' or 'Start Up Items'... this is on Windows 2k and
    > xp. Below is an example of what I'm trying to do.
    >
    > TIA
    >
    > def ipconfig_email():
    > from email.MIMEText import MIMEText
    > import smtplib
    > import time
    > import os
    >
    > u = "user name" #Change This to user's name.
    > f = "my-email-addy"
    > t = "my-email-addy"
    >
    > fp0 = os.popen("ipconfig /all", "r")
    > fp1 = os.popen("psinfo -d -s", "rb")
    > msg = MIMEText(fp0.read() + fp1.read())
    > fp0.close()
    > fp1.close()
    >
    > msg["Subject"] = "%s's IPconfig Report" % u
    > msg["From"] = f
    > msg["To"] = t
    >
    > h = "my.smtp.server"
    > s = smtplib.SMTP(h)
    > s.sendmail(f, t, msg.as_string())
    > s.quit()
    > time.sleep(86400) #24 hour sleep
    > HOW_DO_I_CALL_THE_FUNCTION_AGAIN?
    >
    > ipconfig_email()
    >


    I figured it out. I added this to the function definition:
    ipconfig_email()

    I feel like an ass, sorry to bother you guys!
    Bart Nessux, Jan 13, 2004
    #2
    1. Advertising

  3. Bart Nessux

    Peter Otten Guest

    Bart Nessux wrote:

    > Bart Nessux wrote:
    >> How do I make a function call itself every 24 hours. Also, is there a
    >> way to start the program automatically w/o depending on the OS functions
    >> like 'Task Scheduler' or 'Start Up Items'... this is on Windows 2k and
    >> xp. Below is an example of what I'm trying to do.


    [...]

    > I figured it out. I added this to the function definition:
    > ipconfig_email()


    Note that Python has a limit for nesting functions:

    >>> depth = 0
    >>> def callself():

    .... global depth
    .... depth += 1
    .... callself()
    ....
    >>> try:

    .... callself()
    .... except RuntimeError, e:
    .... print "depth", depth
    .... print e
    ....
    depth 999
    maximum recursion depth exceeded

    >>> 999/365.25

    2.7351129363449691

    This means that your app will probably crash in less than three years.
    Would that be a problem on W2K ?

    If so, a loop could go much longer:

    while True:
    ipconfig_email()

    Seriously, you should reconsider the OS features.

    Peter
    Peter Otten, Jan 13, 2004
    #3
  4. Bart Nessux

    Bart Nessux Guest

    Peter Otten wrote:
    > Bart Nessux wrote:
    >
    >
    >>Bart Nessux wrote:
    >>
    >>>How do I make a function call itself every 24 hours. Also, is there a
    >>>way to start the program automatically w/o depending on the OS functions
    >>>like 'Task Scheduler' or 'Start Up Items'... this is on Windows 2k and
    >>>xp. Below is an example of what I'm trying to do.

    >
    >
    > [...]
    >
    >
    >>I figured it out. I added this to the function definition:
    >>ipconfig_email()

    >
    >
    > Note that Python has a limit for nesting functions:
    >
    >
    >>>>depth = 0
    >>>>def callself():

    >
    > ... global depth
    > ... depth += 1
    > ... callself()
    > ...
    >
    >>>>try:

    >
    > ... callself()
    > ... except RuntimeError, e:
    > ... print "depth", depth
    > ... print e
    > ...
    > depth 999
    > maximum recursion depth exceeded
    >
    >
    >>>>999/365.25

    >
    > 2.7351129363449691
    >
    > This means that your app will probably crash in less than three years.
    > Would that be a problem on W2K ?
    >
    > If so, a loop could go much longer:
    >
    > while True:
    > ipconfig_email()
    >
    > Seriously, you should reconsider the OS features.
    >
    > Peter
    >


    The computers are turned off and on... the script starts at boot. I
    don't think I'll hit a limit, do you?
    Bart Nessux, Jan 13, 2004
    #4
  5. Bart Nessux

    Peter Otten Guest

    Bart Nessux wrote:

    > Peter Otten wrote:
    >> Bart Nessux wrote:
    >>
    >>
    >>>Bart Nessux wrote:
    >>>
    >>>>How do I make a function call itself every 24 hours. Also, is there a
    >>>>way to start the program automatically w/o depending on the OS functions
    >>>>like 'Task Scheduler' or 'Start Up Items'... this is on Windows 2k and
    >>>>xp. Below is an example of what I'm trying to do.

    >>
    >>
    >> [...]
    >>
    >>
    >>>I figured it out. I added this to the function definition:
    >>>ipconfig_email()

    >>
    >>
    >> Note that Python has a limit for nesting functions:
    >>
    >>
    >>>>>depth = 0
    >>>>>def callself():

    >>
    >> ... global depth
    >> ... depth += 1
    >> ... callself()
    >> ...
    >>
    >>>>>try:

    >>
    >> ... callself()
    >> ... except RuntimeError, e:
    >> ... print "depth", depth
    >> ... print e
    >> ...
    >> depth 999
    >> maximum recursion depth exceeded
    >>
    >>
    >>>>>999/365.25

    >>
    >> 2.7351129363449691
    >>
    >> This means that your app will probably crash in less than three years.
    >> Would that be a problem on W2K ?
    >>
    >> If so, a loop could go much longer:
    >>
    >> while True:
    >> ipconfig_email()
    >>
    >> Seriously, you should reconsider the OS features.
    >>
    >> Peter
    >>

    >
    > The computers are turned off and on... the script starts at boot. I
    > don't think I'll hit a limit, do you?


    No, I just wanted to warn you that your recipe may fail when applied on
    tasks that are invoked with higher frequency - and of course test your
    confidence in W2K :)

    Peter
    Peter Otten, Jan 16, 2004
    #5
  6. Bart Nessux <> writes:

    > How do I make a function call itself every 24 hours. Also, is there a
    > way to start the program automatically w/o depending on the OS
    > functions like 'Task Scheduler' or 'Start Up Items'... this is on
    > Windows 2k and xp. Below is an example of what I'm trying to do.

    [snip]

    Never seen twisted? Here is a little tip:

    from twisted.internet import reactor
    def doSomething(mystring):
    print mystring
    reactor.callLater(5, doSomething, mystring)
    doSomething("ciao")
    reactor.run()

    Twisted also has a lot of already implemented protocols for almost
    everything related to net so maybe you can think of a new version of
    your app using twisted's protocols.

    www.twistedmatrix.com

    --
    Valentino Volonghi, Regia SpA, Milan
    Linux User #310274, Gentoo Proud User
    Valentino Volonghi aka Dialtone, Jan 16, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack Wright
    Replies:
    2
    Views:
    6,921
    yeswanthi
    May 28, 2007
  2. Pieter Claerhout
    Replies:
    0
    Views:
    422
    Pieter Claerhout
    Jan 13, 2004
  3. vasanth kumar
    Replies:
    1
    Views:
    116
    Evertjan.
    Oct 3, 2004
  4. Paul
    Replies:
    2
    Views:
    149
  5. K
    Replies:
    4
    Views:
    317
Loading...

Share This Page