【问题标题】:How to schedule Python Code Run Multiple Time Every Day, With using only python code如何安排 Python 代码每天多次运行,仅使用 Python 代码
【发布时间】:2022-01-04 14:55:45
【问题描述】:

我有一个 CSV 文件,其中提到了所有时间,我想每天按时运行我的 python 代码文件,该文件在 CSV 文件中给出。

我在 Schedule 库上看到了一些代码,但我不明白我应该如何编写该代码。 另外,我必须在我想运行的同一个文件中编写计划代码,还是应该创建新文件并在那里编写计划代码并提供我想运行的文件链接?

我的文件名是 - send_mesaage.ipynb

【问题讨论】:

  • 您使用的是什么平台?这似乎是 cron 作业或 Windows 计划任务的用例。
  • @Axe319 我的机器是 Windows,但我不想使用 Windows 任务调度程序,我想每次都运行我在 excel 表中给出的代码。
  • 据我了解,您想在 CSV 中睡到下一次。这可以通过遍历您的时间戳来完成。然后也许利用SO问题In Python, how can I put a thread to sleep until a specific time?中找到的解决方案之一

标签: python scheduled-tasks schedule


【解决方案1】:

来自geeksforgeeks 我发现了这个:

import sched
import time

# instance is created
scheduler = sched.scheduler(time.time,
                            time.sleep)

# function to print time
# and name of the event
def print_event(name):
    print('EVENT:', time.time(), name)

# printing starting time
print ('START:', time.time())

# first event with delay of
# 1 second
e1 = scheduler.enter(1, 1,
                    print_event, ('1 st', ))

# second event with delay of
# 2 seconds
e1 = scheduler.enter(2, 1,
                    print_event, (' 2nd', ))

# executing the events
scheduler.run()

这是如何工作的: 很简单,
event_schedule.enter(Sleep_Time, Priority, Function_to_run)
event_schedule.run() 启动event_schedule.enter() 你有吗

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    • 2015-04-28
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    相关资源
    最近更新 更多