[英]Take a break every hour for a python script
我有一個需要運行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)
子進程在總時間結束時結束。 但它確實每隔一小時就會休息一次。 任何想法都會非常感激。
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")
我復制並添加了我曾寫過的一些代碼。 以此為基礎,根據您的具體需求進行構建。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.