gpt4 book ai didi

python - Python中有定时执行器服务吗?

转载 作者:行者123 更新时间:2023-12-02 08:36:03 25 4
gpt4 key购买 nike

一个Python新手问题。 python 中是否有相当于 Java 的 Scheduled executor 服务?

最佳答案

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

关于python - Python中有定时执行器服务吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14817396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com