简体   繁体   English

Python-什么是运行基于时间的方法/函数的最佳方法。 (24小时)

[英]Python - Whats the best way to run a method/function based on Time. (24h)

I have been thinking to run a script/code on a time that I want. 我一直在考虑在想要的时间运行脚本/代码。 Like we say etc. I want to run a program at 00:00 (I enter it in the code that I want to start at that time) meanwhile it's 23:21. 就像我们说的那样。我想在00:00运行一个程序(我在当时要启动的代码中输入它),同时它是23:21。 So it should wait until that time and then run the method/function or whatever it should do. 因此,它应该等到那个时候再运行方法/函数或应该执行的任何操作。

What would be in that case the best way to run something like this in that case :) ? 在这种情况下,运行这种情况的最佳方式是什么:)?

EDIT: Exemple - 编辑:例如-

I was thinking like to do it through a code. 我当时想通过代码来实现。 Like etc. I have a code with we say 3 functions. 像等。我有一个代码,我们说了3个功能。 At the time 23:50 I want function nr 1 to run. 在23:50的时候,我要运行函数nr 1。 Then at 23:55 i want the function nr 2 to run and then 00:05 i want the function nr3 to run and all that in the same py file. 然后在23:55我希望功能nr 2运行,然后在00:05我希望功能nr3运行,所有这些都在同一个py文件中。

You can use crontab . 您可以使用crontab

To edit: 编辑:

crontab -e

To add initial time: 要添加初始时间:

50 23 * * * /usr/bin/python /your/file.py function1
55 23 * * * /usr/bin/python /your/file.py function2
05 00 * * * /usr/bin/python /your/file.py function3

And it should be like this your file. 它应该像这样您的文件。

import sys

def function1():
    print "function 1 running"
def function2():
    print "function 2 running"
def function3():
    print "function 3 running"

if sys.argv[1]:
    run = sys.argv[1]
    if run == "function1":
      function1()
    elif run == "function2":
       function2()
    elif run == "function3":
       function3()

sched library is what you are looking for. sched库是您所需要的。 No site-packages or another utilities like cron are required. 不需要站点包或其他实用程序,例如cron。

You can do something like this: 您可以执行以下操作:

import time
starttimes = { (23, 50): func1, (23, 55): func2, (0, 5): func3 }

while True:
    now = tuple(time.gmtime()[3:5])
    if now in starttimes:
        starttimes[now]() # call a function 
    time.sleep(60)

Mind that it is a very crude solution, assuming in particular that none of func1func3 functions will run for longer time than the period left to the starting time of the next function. 请注意,这是一个非常粗糙的解决方案,尤其要假设func1func3函数中的任何一个都不会比下一个函数的启动时间所剩的时间更长。 Also: you need to start your program and leave it running all the time. 另外:您需要启动程序并使它始终保持运行状态。 You kill it (or reboot your system), you need to start it again. 您将其杀死(或重新启动系统),则需要重新启动它。

You are looking for a dedicated task scheduler like Celer y. 您正在寻找专用的任务计划程序,例如Celer y。

Or to do it simply, you can put a crontab to run a python script to run every minute. 或者简单地做到这一点,您可以放置​​crontab来运行python脚本,以每分钟运行一次。 Store method name and time to run in a database. 存储方法名称和在数据库中运行的时间。 When the task runs, it sees if there is any task to be run for the minute,and executes the method specified in the database. 任务运行时,它将查看是否有任何任务要运行,并执行数据库中指定的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM