Python中是否有调度执行器服务?

2024-04-26 08:07:41 发布

您现在位置:Python中文网/ 问答频道 /正文

一个python新手的问题。在python中是否有一个等价于Java的Scheduled executor服务?在


Tags: java等价新手executorscheduled
3条回答

有趣的是,我在读你的问题前几分钟看到了这个github包:

Python job scheduling for humans.

自述文件中的示例:

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)

你可以试试看:

^{pr2}$

我的快速研究表明,Java的调度执行器允许命令在延迟或周期性(http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html)后运行。在

Python在标准库中没有任何东西可以处理周期性执行,但是sched模块可以在指定的延迟之后执行任务:http://docs.python.org/2/library/sched.html

关于周期性事件,这里提出了一个类似的问题:Schedule a repeating event in Python 3(不过,python3)。在

threading.timer不做你想做的吗?在

def hello():
    print "hello, world"

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

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

^{pr2}$

产生:

 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 {}

相关问题 更多 >