一個python新手問題。在python中是否有與Java的Scheduled executor服務相當的功能?Python中是否有Scheduled Executor服務?
回答
我的快速研究表明,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
類似的問題在這裏問週期性事件:Schedule a repeating event in Python 3(Python 3中,雖然)。
不是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 {}
我需要爲每N秒運行一次任務,我最終使用了扭曲的框架。 – 2013-02-12 02:28:44
我擴展的例子來顯示如何重複/重置''Timer'' – sotapme 2013-02-12 09:42:22
這是非常類似於這裏的答案http://stackoverflow.com/questions/2398661/schedule-a-repeating-event-in-python-3 。鏈接任務的作品,所以我upvoted你,但不是很乾淨。扭曲有一個更乾淨的界面。 – 2013-02-12 14:27:05
有趣的是,我看到這個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
- 1. Executor服務中的依賴線程 - Java
- 2. 如何有效處理來自Executor服務的多個結果
- 3. 監控並重新啓動Executor服務和將來的任務?
- 4. Powershell:是否有可能有服務取決於遠程服務
- 5. spring @Scheduled 4任務重疊
- 6. ProgrammableWeb本身是否有Web服務API?
- 7. 是否有AJAX服務用於doubleclick(DART)
- 8. .NET - Web服務是否有屬性[DataContract]?
- 9. 我的網絡服務是否有效?
- 10. 是否有一個PHP SVN服務器
- 11. 是否有Web服務的標準URL?
- 12. 是否有某種RSS搜索服務?
- 13. 是否有任何GitHub補充服務?
- 14. 檢查是否有從Windows服務
- 15. Facebook是否有像服務的Gravatar
- 16. 動態更改提交給Executor服務的命令列表
- 17. Azure AD是否有服務用作auth服務器?
- 18. 是否有可能請求angularj服務器在nodejs服務器
- 19. 視頻服務服務器是否有簡稱?
- 20. 如何在python中檢測http和https服務是否正常?
- 21. 是否有檢查WCF服務是否在線的示例?
- 22. Spring Boot ASync + AutoWired Executor vs manual Executor + Future/Callable?
- 23. 是否有可能在Tomcat服務器中擁有XMPP模塊?
- 24. 是否有在Python
- 25. @Scheduled沒有具體日期
- 26. 是否有可能沒有應用程序服務器的JMS服務器?
- 27. 是否有可能確定程序集是否在Web服務中加載?
- 28. 是否可以從另一臺服務器「中繼」WCF服務?
- 29. Python - 檢查隊列中的所有任務是否完成
- 30. 是否有可能在Azure WebRole(MVC)中託管WCF服務
這些問題是相似的,有建議的解決方案: - 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
不,沒有在標準庫中。不過你可能會在PyPI上找到一些東西。 – 2013-02-11 17:25:47