运行Python程序直到指定的时间
我想在jupyter笔记本中运行我的程序,该程序在特定时间停止(例如18:00)。我用while循环和增量索引编写程序,但最好用时间参数编写 我每天运行上述程序7小时。它必须不停地运行运行Python程序直到指定的时间,python,time,while-loop,jupyter-notebook,Python,Time,While Loop,Jupyter Notebook,我想在jupyter笔记本中运行我的程序,该程序在特定时间停止(例如18:00)。我用while循环和增量索引编写程序,但最好用时间参数编写 我每天运行上述程序7小时。它必须不停地运行 while(i<500000): execute algorithm i+=1 导入日期时间 然后,您可以使用各种函数(time或timedelta)来设置时间 timeNow=datetime.datetime() 打印时间现在导入日期时间 然后,您可以使
while(i<500000):
execute algorithm
i+=1
导入日期时间
然后,您可以使用各种函数(time或timedelta)来设置时间
timeNow=datetime.datetime()
打印时间现在导入日期时间
然后,您可以使用各种函数(time或timedelta)来设置时间
timeNow=datetime.datetime()
print timeNow您可以将其设置为cron作业,并在时间x启动作业,在时间x停止。您可以将其设置为cron作业,并在时间x启动作业,在时间x停止。您可以创建一个以小时和分钟为参数的函数,并在
循环中执行检查:
import datetime
while datetime.datetime.now().hour < 18:
do stuff...
import datetime
def proc(h, m):
while True:
currentHour = datetime.datetime.now().hour
currentMinute = datetime.datetime.now().minute
if currentHour == h and currentMinute == m:
break
# Do stuff...
# Function call.
proc(18,0)
您可以创建一个以小时和分钟为参数的函数,并在while
循环中执行检查:
import datetime
def proc(h, m):
while True:
currentHour = datetime.datetime.now().hour
currentMinute = datetime.datetime.now().minute
if currentHour == h and currentMinute == m:
break
# Do stuff...
# Function call.
proc(18,0)
使用:
在期间:
while alarm < datetime.datetime.now().time():
do something
有关更多信息,请查看。的文档使用:
在期间:
while alarm < datetime.datetime.now().time():
do something
有关详细信息,请查看的文档。您可以创建一个子进程,该子进程将在特定时间终止父进程及其自身:
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 ...
您可以创建在特定时间终止父进程及其自身的子进程:
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 ...
假设您希望代码每天运行10个小时(22:oo)。
如果您使用的是Linux,您可以这样做以root用户身份运行作业
sudo crontab -e
0 22 * * * /path/to/directory/python my_code.py
您的python文件my_code.py
可能是这样的
# python code to search pattern in a string using regex
import re
str1 = 'this is {new} string with [special] words.'
r = re.search(r'\{(.*?)\}', str1)
if r:
found =r.group()
else:
"No match found"
print found
假设您希望代码每天运行10个小时(22:oo)。
如果您使用的是Linux,您可以这样做以root用户身份运行作业
sudo crontab -e
0 22 * * * /path/to/directory/python my_code.py
您的python文件my_code.py
可能是这样的
# python code to search pattern in a string using regex
import re
str1 = 'this is {new} string with [special] words.'
r = re.search(r'\{(.*?)\}', str1)
if r:
found =r.group()
else:
"No match found"
print found
您好,欢迎光临。您能为您的问题添加更多技术细节以帮助重现问题吗?您好,欢迎光临。您能为您的问题添加更多技术细节以帮助重现我使用的问题吗。这很有用!谢谢,我用过这个。这很有用!谢谢