【问题标题】:Take a break every hour for a python script每隔一小时就为一个 python 脚本休息一下
【发布时间】:2017-06-21 10:29:17
【问题描述】:

我有一个需要运行 10 小时的脚本。每隔一小时,我想休息 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)

子进程在总时间完成时结束。但它确实需要每隔一小时休息一次。任何想法都将不胜感激。

【问题讨论】:

  • 您可能不会在休息时间准确地获得时间事件。将条件if time.time() == take_break: 更改为if time.time() &gt;= take_break:,程序可能会执行您想要的操作。
  • @JohanL 我认为这应该是一个答案
  • @e4c5 可能,但原始代码还有一些奇怪之处,我不想进一步检查,因此可能需要进一步修改。
  • 感谢@JohanL 的帮助。我试过你的答案,但它并没有停止这个过程。
  • 好吧,您可能还需要在某个地方输入time.sleep(30)

标签: python python-2.7 subprocess multiprocessing


【解决方案1】:
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")

我复制并添加了一些我曾经写过的代码。以此为基础,并根据您的具体需求进行构建。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    • 2023-03-28
    • 2020-10-03
    • 2011-05-16
    • 2019-01-19
    • 1970-01-01
    • 2021-05-04
    相关资源
    最近更新 更多