【发布时间】:2016-03-30 02:09:05
【问题描述】:
我正在尝试生成一些随机播种时间来告诉我的脚本何时从主脚本中触发每个脚本。
我想设置一个时间范围:
START_TIME = "02:00"
END_TIME = "03:00"
到了开始时间,它需要看看我们要运行多少个脚本:
script1.do_proc()
script2.alter()
script3.noneex()
在这种情况下要运行 3 个,因此它需要生成 3 个随机时间来启动这些脚本,每个脚本之间的最小间隔为 5 分钟,但时间必须在设置的时间范围内在START_TIME 和END_TIME
但是,它还需要知道script1.main 总是第一个触发的脚本,其他脚本可以随机播放(随机)
所以我们可能让script1 在 01:43 运行,然后 script3 在 01:55 运行,然后 script2 可能在 02:59 运行
我们还可能让script1 在 01:35 运行,然后 script3 在 01:45 运行,然后 script2 可能在 01:45 运行,这也可以。
到目前为止,我的脚本可以在下面找到:
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)
编辑 2016 年 3 月 31 日
假设我添加以下内容
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倍 - 生成 3 颗种子,确保最短时间为
300,并且 3 颗种子加在一起不得超过TIME_DIFFERENCE - 根据
RUN_TIME = START_TIME和RUN_TIME = RUN_TIME + SEED[i]创建开始时间 - 第一个循环将检查
MAIN_SCRIPT是否存在于SCRIPTS中,如果存在则它将首先运行该脚本,从SCRIPTS中删除自身,然后在下一个循环中,因为它在@987654342 中不存在@它会切换到随机调用其他脚本之一。
播种时代
以下似乎可行,但可能有更简单的方法。
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
【问题讨论】:
-
是脚本变化的数量,我的意思不是3或4,但可以更多?
-
@minhhn2910 是的,确实如此。有没有办法可以将函数放入数组中以计算出我需要多少种子并改组脚本(script1 除外)
-
您使用的是哪个操作系统?
-
我的最后一个问题(问了很多问题,因为不够清楚):在
START_TIME和END_TIME之间,您需要运行所有脚本还是其中一些?我认为很难像你想要的那样生成可以容纳所有脚本的随机时间。 (因为没有设置最大间隔,而最小是5分钟) -
@minhhn2910 在
START_TIME和END TIME之间的时间内,所有脚本都需要运行,但每个脚本只需要运行一次。
标签: python python-2.7 datetime random random-seed