【问题标题】:Run Python Program Until Specific Time运行 Python 程序直到特定时间
【发布时间】:2019-08-12 16:57:26
【问题描述】:

我想在 jupyter notebook 中运行我的程序,该程序在特定时间停止(例如 18:00)。我用while循环和增量索引写程序,但最好用时间参数写。

我每天运行上述程序 7 小时。它必须不间断地运行。

    while(i<500000):
         execute algorithm
         i+=1

但我想像下面这样运行我的程序:

    while(not 18:00 clock):
         execute algorithm

【问题讨论】:

  • 您好,欢迎您,您能否在您的问题中添加更多技术细节以帮助重现该问题

标签: python time while-loop jupyter-notebook


【解决方案1】:

您可以创建一个子进程,该子进程将在特定时间终止父进程及其自身:

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 ...

【讨论】:

    【解决方案2】:

    用途:

    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.
    

    更多信息,请查看datetime的文档。

    【讨论】:

    • 我用过这个。这很有用!谢谢
    【解决方案3】:
    import datetime
    
    while datetime.datetime.now().hour < 18:
        do stuff...
    

    if datetime.datetime.now().hour >= 18:
        return
    

    【讨论】:

      【解决方案4】:

      您可以创建一个以小时和分钟为参数的函数,并在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)
      

      【讨论】:

        【解决方案5】:

        导入日期时间

        https://docs.python.org/3/library/datetime.html

        然后您可以使用各种函数(time 或 timedelta)来设置时间。

        timeNow = datetime.datetime() 现在打印时间

        【讨论】:

          【解决方案6】:

          您可以将其设置为 cron 作业,并在时间 x 开始作业并在时间 x 停止。

          【讨论】:

            【解决方案7】:

            让我们假设您希望您的代码每天运行 10 pm(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
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2011-09-06
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多