2013-02-11 59 views
2

一個python新手問題。在python中是否有與Java的Scheduled executor服務相當的功能?Python中是否有Scheduled Executor服務?

+0

這些問題是相似的,有建議的解決方案: - http://stackoverflow.com/questions/373335/suggestions-for-a-cron-like-scheduler -in-python - http://stackoverflow.com/questions/2398661/schedule-a-repeating-event-in-python-3 – dbader 2013-05-28 08:03:37

+0

不,沒有在標準庫中。不過你可能會在PyPI上找到一些東西。 – 2013-02-11 17:25:47

回答

1

不是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 {} 
+0

我需要爲每N秒運行一次任務,我最終使用了扭曲的框架。 – 2013-02-12 02:28:44

+0

我擴展的例子來顯示如何重複/重置''Timer'' – sotapme 2013-02-12 09:42:22

+0

這是非常類似於這裏的答案http://stackoverflow.com/questions/2398661/schedule-a-repeating-event-in-python-3 。鏈接任務的作品,所以我upvoted你,但不是很乾淨。扭曲有一個更乾淨的界面。 – 2013-02-12 14:27:05

1

有趣的是,我看到這個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) 

你可能想給它一個鏡頭:

$ pip install schedule 
相關問題