• <tfoot id='eXNq5'></tfoot>
      <bdo id='eXNq5'></bdo><ul id='eXNq5'></ul>
      <legend id='eXNq5'><style id='eXNq5'><dir id='eXNq5'><q id='eXNq5'></q></dir></style></legend>

        <small id='eXNq5'></small><noframes id='eXNq5'>

        <i id='eXNq5'><tr id='eXNq5'><dt id='eXNq5'><q id='eXNq5'><span id='eXNq5'><b id='eXNq5'><form id='eXNq5'><ins id='eXNq5'></ins><ul id='eXNq5'></ul><sub id='eXNq5'></sub></form><legend id='eXNq5'></legend><bdo id='eXNq5'><pre id='eXNq5'><center id='eXNq5'></center></pre></bdo></b><th id='eXNq5'></th></span></q></dt></tr></i><div id='eXNq5'><tfoot id='eXNq5'></tfoot><dl id='eXNq5'><fieldset id='eXNq5'></fieldset></dl></div>
      1. 安排 Python 脚本每小时准确运行

        时间:2024-04-21

      2. <tfoot id='GM6Q9'></tfoot>

          <tbody id='GM6Q9'></tbody>
      3. <legend id='GM6Q9'><style id='GM6Q9'><dir id='GM6Q9'><q id='GM6Q9'></q></dir></style></legend>
            <i id='GM6Q9'><tr id='GM6Q9'><dt id='GM6Q9'><q id='GM6Q9'><span id='GM6Q9'><b id='GM6Q9'><form id='GM6Q9'><ins id='GM6Q9'></ins><ul id='GM6Q9'></ul><sub id='GM6Q9'></sub></form><legend id='GM6Q9'></legend><bdo id='GM6Q9'><pre id='GM6Q9'><center id='GM6Q9'></center></pre></bdo></b><th id='GM6Q9'></th></span></q></dt></tr></i><div id='GM6Q9'><tfoot id='GM6Q9'></tfoot><dl id='GM6Q9'><fieldset id='GM6Q9'></fieldset></dl></div>
            1. <small id='GM6Q9'></small><noframes id='GM6Q9'>

                <bdo id='GM6Q9'></bdo><ul id='GM6Q9'></ul>
                • 本文介绍了安排 Python 脚本每小时准确运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在我问之前,Cron Jobs 和 Task Scheduler 将是我最后的选择,这个脚本将在 Windows 和 Linux 上使用,我更喜欢有一个编码的方法来做这件事而不是离开这交给最终用户来完成.

                  Before I ask, Cron Jobs and Task Scheduler will be my last options, this script will be used across Windows and Linux and I'd prefer to have a coded out method of doing this than leaving this to the end user to complete.

                  是否有可用于安排任务的 Python 库?我需要每小时运行一次函数,但是,如果我每小时运行一次脚本并使用 .sleep,每小时一次",随着时间的推移,我将需要运行一次函数.由于执行/运行脚本和/或函数固有的延迟,将在与前一天不同的时间运行.

                  Is there a library for Python that I can use to schedule tasks? I will need to run a function once every hour, however, over time if I run a script once every hour and use .sleep, "once every hour" will run at a different part of the hour from the previous day due to the delay inherent to executing/running the script and/or function.

                  什么是最好的方法来安排一个函数在一天中的特定时间运行(不止一次)不使用 Cron 作业或使用任务计划程序来安排它?

                  What is the best way to schedule a function to run at a specific time of day (more than once) without using a Cron Job or scheduling it with Task Scheduler?

                  或者,如果这不可能,我也希望得到您的意见.

                  import datetime
                  import time
                  from apscheduler.scheduler import Scheduler
                  
                  # Start the scheduler
                  sched = Scheduler()
                  sched.daemonic = False
                  sched.start()
                  
                  def job_function():
                      print("Hello World")
                      print(datetime.datetime.now())
                      time.sleep(20)
                  
                  # Schedules job_function to be run once each minute
                  sched.add_cron_job(job_function,  minute='0-59')
                  

                  出来:

                  >Hello World
                  >2014-03-28 09:44:00.016.492
                  >Hello World
                  >2014-03-28 09:45:00.0.14110
                  

                  版本 >3.0

                  (来自以下 Animesh Pandey 的回答)

                  Version > 3.0

                  (From Animesh Pandey's answer below)

                  from apscheduler.schedulers.blocking import BlockingScheduler
                  
                  sched = BlockingScheduler()
                  
                  @sched.scheduled_job('interval', seconds=10)
                  def timed_job():
                      print('This job is run every 10 seconds.')
                  
                  @sched.scheduled_job('cron', day_of_week='mon-fri', hour=10)
                  def scheduled_job():
                      print('This job is run every weekday at 10am.')
                  
                  sched.configure(options_from_ini_file)
                  sched.start()
                  

                  推荐答案

                  也许这会有所帮助:Advanced Python Scheduler

                  这是他们文档中的一小段代码:

                  Here's a small piece of code from their documentation:

                  from apscheduler.schedulers.blocking import BlockingScheduler
                  
                  def some_job():
                      print "Decorated job"
                  
                  scheduler = BlockingScheduler()
                  scheduler.add_job(some_job, 'interval', hours=1)
                  scheduler.start()
                  

                  这篇关于安排 Python 脚本每小时准确运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Python 中使用 try/except 将字符串转换为 Int 下一篇:pip 或 pip3 为 Python 3 安装包?

                  相关文章

                    <bdo id='0xWo1'></bdo><ul id='0xWo1'></ul>

                  <small id='0xWo1'></small><noframes id='0xWo1'>

                • <tfoot id='0xWo1'></tfoot>

                • <i id='0xWo1'><tr id='0xWo1'><dt id='0xWo1'><q id='0xWo1'><span id='0xWo1'><b id='0xWo1'><form id='0xWo1'><ins id='0xWo1'></ins><ul id='0xWo1'></ul><sub id='0xWo1'></sub></form><legend id='0xWo1'></legend><bdo id='0xWo1'><pre id='0xWo1'><center id='0xWo1'></center></pre></bdo></b><th id='0xWo1'></th></span></q></dt></tr></i><div id='0xWo1'><tfoot id='0xWo1'></tfoot><dl id='0xWo1'><fieldset id='0xWo1'></fieldset></dl></div>
                  1. <legend id='0xWo1'><style id='0xWo1'><dir id='0xWo1'><q id='0xWo1'></q></dir></style></legend>