Python 中有调度执行器服务吗?

4 投票
3 回答
6729 浏览
提问于 2025-04-17 15:31

一个Python新手的问题。Python有没有类似于Java的定时执行服务的东西?

3 个回答

1

有趣的是,在我看到你提问的几分钟前,我刚好看到了这个GitHub上的包:

为人类设计的Python任务调度。

下面是从说明文档中摘取的一个例子:

import schedule

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

schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)

while 1:
    schedule.run_pending()
    time.sleep(1)

你可能想试试看这个:

$ pip install schedule
4

难道threading.timer不是可以满足你的需求吗?

def hello():
    print "hello, world"

t = Timer(30.0, hello)
t.start() # after 30 seconds, "hello, world" will be printed

编辑 添加了可重复的示例。

import threading
import time

def repeat_every(n, func, *args, **kwargs):
    def and_again():
        func(*args, **kwargs)
        t = threading.Timer(n, and_again)
        t.daemon = True
        t.start()
    t = threading.Timer(n, and_again)
    t.daemon = True
    t.start()


def scheduled_task(msg='hello, world', **kwargs):
    print time.time(), "scheduled_task:", msg, kwargs

repeat_every(.5, scheduled_task )
repeat_every(1, scheduled_task, "Slow", name="Hand luke")

for x in range(5):
    print time.time(), "Main: busy as a bee."
    time.sleep(3)

生成:

 1360662042.34 Main: busy as a bee.
1360662042.84 scheduled_task: hello, world {}
1360662043.34 scheduled_task: Slow {'name': 'Hand luke'}
1360662043.34 scheduled_task: hello, world {}
1360662043.84 scheduled_task: hello, world {}
1360662044.34 scheduled_task: Slow {'name': 'Hand luke'}
1360662044.34 scheduled_task: hello, world {}
1360662044.84 scheduled_task: hello, world {}
1360662045.34 Main: busy as a bee.
1360662045.34 scheduled_task: Slow {'name': 'Hand luke'}
1360662045.34 scheduled_task: hello, world {}
1360662045.85 scheduled_task: hello, world {}
1360662046.34 scheduled_task: Slow {'name': 'Hand luke'}
1360662046.35 scheduled_task: hello, world {}
1360662046.85 scheduled_task: hello, world {}
1360662047.34 scheduled_task: Slow {'name': 'Hand luke'}
1360662047.35 scheduled_task: hello, world {}
1360662047.85 scheduled_task: hello, world {}
1360662048.34 Main: busy as a bee.
1360662048.34 scheduled_task: Slow {'name': 'Hand luke'}
1360662048.35 scheduled_task: hello, world {}
1360662048.85 scheduled_task: hello, world {}
1360662049.35 scheduled_task: Slow {'name': 'Hand luke'}
1360662049.35 scheduled_task: hello, world {}
1360662049.86 scheduled_task: hello, world {}
1360662050.35 scheduled_task: Slow {'name': 'Hand luke'}
1360662050.36 scheduled_task: hello, world {}
1360662050.86 scheduled_task: hello, world {}
1360662051.34 Main: busy as a bee.
1360662051.35 scheduled_task: Slow {'name': 'Hand luke'}
1360662051.36 scheduled_task: hello, world {}
1360662051.86 scheduled_task: hello, world {}
1360662052.35 scheduled_task: Slow {'name': 'Hand luke'}
1360662052.36 scheduled_task: hello, world {}
1360662052.86 scheduled_task: hello, world {}
1360662053.35 scheduled_task: Slow {'name': 'Hand luke'}
1360662053.36 scheduled_task: hello, world {}
1360662053.86 scheduled_task: hello, world {}
1360662054.34 Main: busy as a bee.
1360662054.35 scheduled_task: Slow {'name': 'Hand luke'}
1360662054.37 scheduled_task: hello, world {}
1360662054.87 scheduled_task: hello, world {}
1360662055.36 scheduled_task: Slow {'name': 'Hand luke'}
1360662055.37 scheduled_task: hello, world {}
1360662055.87 scheduled_task: hello, world {}
1360662056.36 scheduled_task: Slow {'name': 'Hand luke'}
1360662056.37 scheduled_task: hello, world {}
1360662056.87 scheduled_task: hello, world {}
1

我简单查了一下,发现Java的Scheduled Executor可以让你设置命令在延迟后执行或者定期执行(你可以查看这个链接了解更多:http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html)。

而Python的标准库里没有直接处理定期执行的功能,不过它有一个叫sched的模块,可以在指定的延迟后执行任务:http://docs.python.org/2/library/sched.html

这里也有人问过类似的问题,关于定期事件的安排:在Python 3中安排重复事件(不过是针对Python 3的)。

撰写回答