我想在jupyter笔记本上运行我的程序,这个程序在特定的时间停止(例如18:00)。我用时间循环和增量索引编写了程序,但最好用时间参数来编写。
我每天运行上述程序7个小时。它必须不间断地运行。
while(i<500000):
execute algorithm
i+=1但我想像这样运行我的程序:
while(not 18:00 clock):
execute algorithm发布于 2019-03-21 18:47:14
import datetime
while datetime.datetime.now().hour < 18:
do stuff...或
if datetime.datetime.now().hour >= 18:
return发布于 2019-03-21 19:06:46
您可以创建一个子进程,该进程将在特定时间终止父进程及其自身:
import multiprocessing as mp
import time
import datetime
import sys
import signal
import os
def process(hr, minute):
while True:
d = datetime.datetime.now()
if d.hour == hr and d.minute == minute:
os.kill(os.getppid(), signal.SIGTERM)
sys.exit()
else:
time.sleep(25)
p = mp.Process(target=process, args=(18, 0))
p.start()
# your program here ...发布于 2019-03-21 18:50:56
使用:
import datetime
#create the alarm clock.
alarm = datetime.time(15, 8, 24) #Hour, minute and second you want.同时:
while alarm < datetime.datetime.now().time():
do something您也可以设置一个特定的日期,设置如下:
datetime.datetime(2019, 3, 21, 22, 0, 0) #Year, month, day, hour, minute and second you want.有关更多信息,请查看日期时间文档。
https://stackoverflow.com/questions/55287184
复制相似问题