python time

ecu_jon hayesjdno3 at yahoo.com
Sun Mar 20 23:29:08 EDT 2011


On Mar 20, 10:48 pm, ecu_jon <hayesjd... at yahoo.com> wrote:
> On Mar 20, 10:09 pm, Dave Angel <da... at ieee.org> wrote:
>
>
>
> > On 01/-10/-28163 02:59 PM, ecu_jon wrote:
>
> > > I'm working on a script that will run all the time. at time specified
> > > in a config file, will kick-off a backup.
> > > problem is, its not actually starting the job. the double while loop
> > > runs, the first comparing date works. the second for hour/min does
> > > not.
>
> > > #python file
> > > import os,string,,time,getpass,ConfigParser
> > > from datetime import *
> > > from os.path import join, getsize
> > > from time import strftime,localtime
>
> > > config = ConfigParser.ConfigParser()
> > > config.read("config.ini")
> > > source = config.get("myvars", "source")
> > > destination = config.get("myvars", "destination")
> > > date = config.get("myvars", "date")
> > > time = config.get("myvars", "time")
> > > str_time=strftime("%H:%M",localtime())
>
> > > while datetime.now().weekday() == int(date):
> > >      while str_time == time:
> > >          print "do it"
>
> > You're comparing two objects in that inner while-loop, but since they
> > never change, they'll never match unless they happen to start out as
> > matched.
>
> > you need to re-evaluate str_time each time through the loop.  Make a
> > copy of that statement and put it inside the loop.
>
> > You probably don't want those loops to be comparing to ==, though, since
> > if you start this script on some other day, it'll never loop at all.
> > Also, it'd be good to do some form of sleep() function when you're
> > waiting, so you don't bog the system down with a busy-loop.
>
> > DaveA
>
> i guess im just having a hard time creating something like
> check if go condition,
> else sleep
>
> the double while loops take 12% cpu usage on my machine so this is
> probably unacceptable.
> also the sleep command does not like me :
>  >>> from datetime import *
>
> >>> from time import strftime,localtime,sleep
> >>> time.sleep(3)
>
> Traceback (most recent call last):
>   File "<pyshell#2>", line 1, in <module>
>     time.sleep(3)
> AttributeError: type object 'datetime.time' has no attribute 'sleep'
>
>
>
> here it is updated with the hour/min check fixed.
> #updated python code
> import os,string,time,getpass,md5,ConfigParser
> from datetime import *
> from os.path import join, getsize
> from time import strftime,localtime,sleep
>
> config = ConfigParser.ConfigParser()
> config.read("config.ini")
> date = config.get("myvars", "date")
> time = config.get("myvars", "time")
>
> while datetime.now().weekday() == int(date):
>     str_time=strftime("%H:%M",localtime())
>     while str_time == time:
>         print "do it"

i think this is what you are talking about
except that the time.sleep just does not work.
even changing "from time import strftime,localtime" to "from time
import strftime,localtime,sleep" does not do it.
#python code
import os,string,time,getpass,md5,ConfigParser
from datetime import *
from os.path import join, getsize
from time import strftime,localtime

config = ConfigParser.ConfigParser()
config.read("config.ini")
date = config.get("myvars", "date")
time = config.get("myvars", "time")

a=1
while a>0:
    if datetime.now().weekday() == int(date):
        str_time=strftime("%H:%M",localtime())
        if str_time == time:
            print "do it"
    time.sleep(58)





More information about the Python-list mailing list