延迟程序流动作不稳定

2024-10-03 19:29:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我对编写python相当陌生,所以如果我看起来不知道自己在做什么,请原谅我。我的目标是实现一个自动视频播放器(这是为我的公司)。上午9:30,任务调度器将启动这个python文件。9:30的密码是这样的:

This script runs all day and plays a commercial using VLC media player every 30 minutes
# Notes:
# Python uses a 24 hour format, so 2:30 PM = 14:30
# All spaces in video files names must be replaced by an underscore i.e. no spaces in the file name
#   ...or this will not work
#   Example: "Example Video Name.wmv" would need to be changed to "Example_Video_Name.wmv"

import time
import os
import subprocess
import datetime

# start here at 9:30AM, windows task scheduler takes care of this
print "It is 9:30, playing first video"
# set our working directory
os.chdir("C:\Commercials")
# the commercial to be played is specified here
commName = "Robotow_Heavy_Duty.wmv"
# get the length in seconds of the commercial
commLength = subprocess.check_output('ffprobe -i ' + commName + ' -show_entries format=duration -v quiet -of csv="p=0"')
# start the file
p = subprocess.Popen(['C:\Program Files\VideoLAN\VLC\\vlc.exe', '-f', 'C:\Commercials\\' + commName])
# delay closing the file until it is finished playing
time.sleep(float(commLength))
# kill the VLC player
p.terminate()

print "finished playing " + commName

所发生的事情是相当好的评论,视频运行,然后被杀死后,它的持续时间已过。之后,以下代码使其休眠到10:

print "finished playing " + commName
# delay workflow until 10:00 AM
print "Waiting until 10:00 to run next commercial..."
for i in xrange(0, 365):
    # sleep until 10:00 AM
    t = datetime.datetime.today()
    future = datetime.datetime(t.year, t.month, t.day, 10, 00)
    if t.hour >= 10 and t.minute >= 00:
        future += datetime.timedelta(days=1)
    time.sleep((future-t).seconds)

然后用一个不同的视频名称重复从一开始的代码,它会一直休眠到10:30。奇怪的是,它会工作大约75%的时间。它会自己去,直到11:00(它不会播放11:00的视频)和其他时间,它会一直到1:30,等等,我的时间使用的结构应该检查。例如,我会这样做,让它一直睡到下午3点:

for i in xrange(0, 365):
# sleep until 3:00 PM
t = datetime.datetime.today()
future = datetime.datetime(t.year, t.month, t.day, 15, 00)
if t.hour >= 15 and t.minute >= 00:
    future += datetime.timedelta(days=1)
time.sleep((future-t).seconds)

我知道这太多了,我为此道歉。如果您有任何意见,我可以做得更好,为什么这是行动起来,我会非常感谢


Tags: thetoinimportdatetime视频timefuture