Make a function call itself after set amount of time

B

Bart Nessux

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()
 
B

Bart Nessux

Bart said:
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!
 
P

Peter Otten

Bart said:
Bart said:
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:
.... global depth
.... depth += 1
.... callself()
........ callself()
.... except RuntimeError, e:
.... print "depth", depth
.... print e
....
depth 999
maximum recursion depth exceeded
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
 
B

Bart Nessux

Peter said:
Bart Nessux wrote:

Bart said:
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:


... global depth
... depth += 1
... callself()
...

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


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?
 
P

Peter Otten

Bart said:
Peter said:
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()
...

... 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
 
V

Valentino Volonghi aka Dialtone

Bart Nessux said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top