我尝试生成一些随机种子时间来告诉我的脚本何时从主脚本中启动每个脚本。在
我想设定一个时间范围:
START_TIME = "02:00"
END_TIME = "03:00"
当到达启动时间时,它需要查看我们必须运行多少脚本:
^{pr2}$在本例中有3个要运行,因此需要生成3个随机时间来启动这些脚本,每个脚本之间间隔5分钟,但时间必须在START_TIME
和{
但是,它还需要知道script1.main
总是第一个启动的脚本,其他脚本可以被随机移动(随机)
所以我们可能会让script1
在01:43运行,然后{
我们也可以让script1
在01:35运行,然后{
到目前为止,我的脚本可以在下面找到:
import random
import pytz
from time import sleep
from datetime import datetime
import script1
import script2
import script3
START_TIME = "01:21"
END_TIME = "03:00"
while 1:
try:
# Set current time & dates for GMT, London
CURRENT_GMTTIME = datetime.now(pytz.timezone('Europe/London')).strftime("%H%M")
CURRENT_GMTDAY = datetime.now(pytz.timezone('Europe/London')).strftime("%d%m%Y")
sleep(5)
# Grab old day for comparisons
try:
with open("DATECHECK.txt", 'rb') as DATECHECK:
OLD_DAY = DATECHECK.read()
except IOError:
with open("DATECHECK.txt", 'wb') as DATECHECK:
DATECHECK.write("0")
OLD_DAY = 0
# Check for new day, if it's a new day do more
if int(CURRENT_GMTDAY) != int(OLD_DAY):
print "New Day"
# Check that we are in the correct period of time to start running
if int(CURRENT_GMTTIME) <= int(START_TIME.replace(":", "")) and int(CURRENT_GMTTIME) >= int(END_TIME.replace(":", "")):
print "Correct time, starting"
# Unsure how to seed the start times for the scripts below
script1.do_proc()
script2.alter()
script3.noneex()
# Unsure how to seed the start times for above
# Save the current day to prevent it from running again today.
with open("DATECHECK.txt", 'wb') as DATECHECK:
DATECHECK.write(CURRENT_GMTDAY)
print "Completed"
else:
pass
else:
pass
except Exception:
print "Error..."
sleep(60)
假设我添加以下内容
SCRIPTS = ["script1.test()", "script2.test()", "script3.test()"]
MAIN_SCRIPT = "script1.test()"
TIME_DIFFERENCE = datetime.strptime(END_TIME, "%H:%M") - datetime.strptime(START_TIME, "%H:%M")
TIME_DIFFERENCE = TIME_DIFFERENCE.seconds
当然,有一种方法我们可以插入某种循环,让它全部完成。。在
for i in range(len(SCRIPTS))
,是3倍300
,并且这3个种子加起来不能超过TIME_DIFFERENCE
RUN_TIME = START_TIME
创建开始时间,然后RUN_TIME = RUN_TIME + SEED[i]
MAIN_SCRIPT
是否存在于SCRIPTS
中,如果存在,那么它将首先运行该脚本,从SCRIPTS
中删除自己,然后在下一个循环中,因为它不存在于SCRIPTS
中,它将切换到随机调用其他脚本之一。在下面的方法似乎有效,但可能有一种更简单的方法来实现这一点。在
CALCULATE_SEEDS = 0
NEW_SEED = 0
SEEDS_SUCESSS = False
SEEDS = []
while SEEDS_SUCESSS == False:
# Generate a new seed number
NEW_SEED = random.randrange(0, TIME_DIFFERENCE)
# Make sure the seed is above the minimum number
if NEW_SEED > 300:
SEEDS.append(NEW_SEED)
# Make sure we have the same amount of seeds as scripts before continuing.
if len(SEEDS) == len(SCRIPTS):
# Calculate all of the seeds together
for SEED in SEEDS:
CALCULATE_SEEDS += SEED
# Make sure the calculated seeds added together is smaller than the total time difference
if CALCULATE_SEEDS >= TIME_DIFFERENCE:
# Reset and try again if it's not below the number
SEEDS = []
else:
# Exit while loop if we have a correct amount of seeds with minimum times.
SEEDS_SUCESSS = True
使用
datetime.timedelta
计算时差。此代码假定所有三个进程在同一天运行rnd1
、rnd2
和{rnd3
的值不能大于总时间间隔(3 * d1 + 600
)。所以这三次都发生在间隔时间内。在注意:您没有指定每个脚本运行的时间。这就是为什么我没有使用
time.sleep
。一个可能的选项是threading.Timer
(请参阅python文档)。在假设您存储了所有方法函数()在数组中,并且,如您所述,后续脚本必须在script1之后至少5分钟。它们可以随机执行,所以我们可以启动多个进程,让它们在自动启动之前休眠一段时间。(计时单位为秒)
现在,您只需在每天的开始时间调用do_my_jobs()一次。在
相关问题 更多 >
编程相关推荐