Question

Updated below:

I have a script which has a function I made that calculates the time to set my script to sleep for based on a few factors, which means the script might not need to run again for a half hour or it could be a few days.

Now, I had thought that time.sleep(seconds) would set the program to sleep and then wake it up and run it again.

time.sleep(3600)
runScriptAgain()

Apparently this doesn't do what I thought it did. Of course most things never seem to do what I think, the joys of learning!

At any rate once I figured out that the script was never resuming I looked around for other ways to do this. Someone had mentioned os.exec but from my (very limited) knowledge it looks like that is used to run something else from within the script. Then I tried the threading.Timer function but that doesn't seem to do what I want either. Or I have no idea what I'm doing, which is even more likely. And then I've seen people mentioning threads and daemons and things that I have no idea about at all.

I was thinking maybe a for loop but that seems pretty amateurish(pot meet kettle!), and I'm not sure how well that would even work(if at all).

timeToSleep = SleepValue()
count = 0 
while count < timeToSleep:
    count + 1
    time.sleep(1)
runscript()

So is there an easy way to set a script to stop running for a specific amount of time, run again, and then sleep until the next time? I figure there must be a pretty simple way to do this, but I really have no clue. I'm on Windows 7 using Python 3.2.3, and I've only been learning python for a few months now so anything complicated is likely to be outside my skill level.

Updated:

def something():
    print('Hello!')

def sleep():
    time.sleep(5)
    runscript()

def runscript():
    something()
    sleep()

runscript()

Ok, that's how my code is currently setup. I tried it with this simple code and it seems the problem is that it just never wakes up? I'm not really sure what's going on. I thought it would call the first function, print the item, then sleep for 5 seconds, and repeat ad naseum. Maybe somebody can explain how I should be doing that.

Thanks!

Était-ce utile?

La solution

You may want to have a look at python's sched module for this or possibly something like Advanced Python Scheduler. Either way, if you want to run the scheduling from within your script you have to set it up as a long running process or service. Ignacio Vazquez-Abrams' idea is the most simple - just use your OS's scheduler (the Window's task scheduler or cron on Linux) to run it every x minutes. Have your script initially read a config file to determine if it should finish executing or exit. If you need to update or change the next time it should run, you just update the config file.

Autres conseils

I set up a script just like this and time.sleep( 3600 ) works for me. Place the run code inside the while loop and then just let it run. No need to compare to some value.

KEEP_GOING = should_we_keep_going( )

while KEEP_GOING:
     Time.sleep( 100 )
     Runscript( )
KEEP_GOING = should_we_keep_going( )

Also I would say the idea of setting up a scheduled task is a fantastic idea.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top