Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python脚本每天在同一时间做一些事情_Python_Python 2.7_Scheduler - Fatal编程技术网

Python脚本每天在同一时间做一些事情

Python脚本每天在同一时间做一些事情,python,python-2.7,scheduler,Python,Python 2.7,Scheduler,我有一个长时间运行的python脚本,我想在每天早上1:00做一些事情 我一直在看模块和对象,但我看不出如何使用它们来实现这一点。您可能正在寻找APScheduler from datetime import date from apscheduler.scheduler import Scheduler # Start the scheduler sched = Scheduler() sched.start() # Define the function that is to be ex

我有一个长时间运行的python脚本,我想在每天早上1:00做一些事情


我一直在看模块和对象,但我看不出如何使用它们来实现这一点。

您可能正在寻找APScheduler

from datetime import date
from apscheduler.scheduler import Scheduler

# Start the scheduler
sched = Scheduler()
sched.start()

# Define the function that is to be executed
def my_job(text):
    print text

# The job will be executed on November 6th, 2009
exec_date = date(2009, 11, 6)

# Store the job in a variable in case we want to cancel it
job = sched.add_date_job(my_job, exec_date, ['text'])

# The job will be executed on November 6th, 2009 at 16:30:05
job = sched.add_date_job(my_job, datetime(2009, 11, 6, 16, 30, 5), ['text'])


通过将它构建到您正在调度的函数中,您可以让它调度另一次运行

您可以这样做:

from datetime import datetime
from threading import Timer

x=datetime.today()
y=x.replace(day=x.day+1, hour=1, minute=0, second=0, microsecond=0)
delta_t=y-x

secs=delta_t.seconds+1

def hello_world():
    print "hello world"
    #...

t = Timer(secs, hello_world)
t.start()
这将在第二天凌晨1点执行一个函数(如hello_world)

编辑:

正如@PaulMag所建议的,更一般地说,为了检测是否由于月底的到来而必须重置月日,在这种情况下y的定义应如下所示:

y = x.replace(day=x.day, hour=1, minute=0, second=0, microsecond=0) + timedelta(days=1)
使用此修复程序,还需要将timedelta添加到导入中。其他代码行保持相同。因此,使用total_seconds()函数的完整解决方案是:

from datetime import datetime, timedelta
from threading import Timer

x=datetime.today()
y = x.replace(day=x.day, hour=1, minute=0, second=0, microsecond=0) + timedelta(days=1)
delta_t=y-x

secs=delta_t.total_seconds()

def hello_world():
    print "hello world"
    #...

t = Timer(secs, hello_world)
t.start()

我花了相当多的时间在01:00启动一个简单的Python程序。出于某种原因,我无法让cron启动它,而APScheduler对于一些简单的东西来说似乎相当复杂。时间表()似乎是对的

您必须安装他们的Python库:

pip install schedule
这是根据他们的示例程序修改的:

import schedule
import time

def job(t):
    print "I'm working...", t
    return

schedule.every().day.at("01:00").do(job,'It is 01:00')

while True:
    schedule.run_pending()
    time.sleep(60) # wait one minute
您需要将自己的函数替换为作业,并使用nohup运行它,例如:

nohup python2.7 MyScheduledProgram.py &

如果重新启动,请不要忘记再次启动。

我需要类似的任务。这是我写的代码: 它计算第二天,将时间更改为所需的时间,并查找当前时间和下一个计划时间之间的秒数

import datetime as dt

def my_job():
    print "hello world"
nextDay = dt.datetime.now() + dt.timedelta(days=1)
dateString = nextDay.strftime('%d-%m-%Y') + " 01-00-00"
newDate = nextDay.strptime(dateString,'%d-%m-%Y %H-%M-%S')
delay = (newDate - dt.datetime.now()).total_seconds()
Timer(delay,my_job,()).start()


也可以考虑运行一个单独的脚本。是的,应该说,不能访问<代码> CRON<代码>。我每天早上都需要这样做……回调可以在第一次调用时启动另一个<代码>定时器< /代码>。修复日期逻辑:x= DATETIME.TY()=(x+TimeDelad(天=1))。替换(小时=2,分钟=0,秒=0)。delta_t=y-xNice解决方案。只是一个问题,为什么要在行中添加1秒
secs=delta\u t.seconds+1
?@Sandi问得好!这是因为使用x=datetime.today(),x可以有0-9999999微秒(除了其他值);然后,使用x-y的秒数得出的结果将在假定日期之前0-999999微秒开始;使用+1时,该功能将在假定日期后0-999999微秒启动。这类似于秒的四舍五入。但是,如果需要更高的精度,“1”可以更改为“0.000001*增量微秒”。这不适用于版本3的apscheduler。有人知道当前语法的类似示例吗?我遇到了问题链接不再适用于python 3现在不适用于python 3。将来看到此线程的任何人,希望他们理解这是生产代码的正确解决方案,而不是公认的解决方案。此解决方案在mac、linux和windows上有效吗?或者这个脚本需要一些修改@是的,我在linux上使用它,它应该在mac上工作。窗户,我不确定。然而,仔细地设置和测试它并不是什么大投资。我建议无论如何,因为调度程序有点棘手。我也可以证明Mac和unix。没有在windows上试用过。这个库在windows上也很有用!嗨,我知道你找到了明天和今天之间的秒数,但是你能解释一下你放在“Timer()”中的那部分吗?这一行的参数是什么意思,()。我查找计时器并发现:您需要为此安装timer3吗?谢谢嗨,你不需要timer3库。我使用了线程中的计时器。第三个参数是将参数传递给函数,即希望计时器在调用时发送给函数的参数。是计时器库使用的另一个参考