简体   繁体   English

每小时休息一下,获取python脚本

[英]Take a break every hour for a python script

I have got a script which needs to run for 10 hours. 我有一个需要运行10个小时的脚本。 For every one hour, I would like take a break for 30 sec and continue the process till test time completed. 每隔一小时,我想休息30秒并继续这个过程直到测试时间结束。

def worker_1(ip, ask_time):

    time_out = time.time() + ask_time*60*60
    photo_directory = os.listdir(photo_path)

    while time.time() < time_out: 
        take_break = time.time() + 60*60

        while time.time() <= take_break:
            #Send command to device

            if time.time() == take_break:

                action = 'DoAction'
                arg_list = []
                arg_list.append(upnp_path)
                arg_list.append(' --action=')
                arg_list.append(action)
                arg_list.append(' --ip=')
                arg_list.append('34.35.35.02')

                x = subprocess.Popen(arg_list, shell=True)
                subprocess.call(["python" , arg_list])

                break 
                print "Sleep time 30 secs ..."

            else:
                for photo in photo_directory:

                    choice_photos_list = list()    
                    image_name = ["john", "Sue", "lee"]    
                    random_choice = random.choice(image_name)

                    if photo.lower().startswith(random_choice):

                        choice_photos_list.append(photo)

                        for choice_photo in choice_photos_list:
                            action = 'CancelAction'
                            arg_list = []
                            arg_list.append(upnp_path)
                            arg_list.append(' --action=')
                            arg_list.append(action)
                            arg_list.append(' --ip=')
                            arg_list.append('34.35.35.02')
                            arg_list.append(' --img=')
                            arg_list.append(choice_photo)

                            x = subprocess.Popen(arg_list, shell=True)

                            subprocess.call(["python" , arg_list])

                            time.sleep(30)

subprocess ends when total time is completed. 子进程在总时间结束时结束。 But it does take break every one hour. 但它确实每隔一小时就会休息一次。 Any ideas would be really appreciated. 任何想法都会非常感激。

def worker_1():
    import time
    Minute, Hourcount, Match = int(time.strftime("%M")), 0, time.strftime("%S")
    while Hourcount < 10:
        #Do Something
        if int(time.strftime("%M")) == Minute:
            if Match == time.strftime("%S"):
                time.sleep(30)
                Hourcount += 1
                Match = time.strftime("%S")

I copied and addapted some code I once wrote. 我复制并添加了我曾写过的一些代码。 Use this as your foundation, and build it up according to your specific needs. 以此为基础,根据您的具体需求进行构建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM