網站首頁 編程語言 正文
前言
sched是Python的內置模塊,用于事件調度,可在安全的在多線程環境中輕松實現定時任務。
sched是一種調度(延時處理機制)。
sched是python內置庫,不需要安裝。
示例代碼:
import sched
import time
from datetime import datetime
# 初始化sched模塊的scheduler類
# 第一個參數是一個可以返回時間戳的函數,第二個參數可以在定時未到達之前阻塞。
schedule = sched.scheduler(time.time, time.sleep)
def task(inc):
now = datetime.now()
ts = now.strftime("%Y-%m-%d %H:%M:%S")
print(ts)
schedule.enter(inc, 0, task, (inc,))
def func(inc=3):
# enter四個參數分別為:
# 間隔事件、優先級(用于同時間到達的兩個事件同時執行時定序)、被調用觸發的函數、給該觸發函數的參數(tuple形式)
schedule.enter(0, 0, task, (inc,))
schedule.run()
func()
運行結果:
補充:解析
主要使用調度器對象 sched.scheduler
調度器對象初始化方法 def __init__(self, timefunc=_time, delayfunc=time.sleep)
- timefunc:經過時間調用的方法,默認為 time.monotonic(),返回單調時鐘的值,單位為小數秒
- delayfunc:延遲時間調用的方法,默認為 time.sleep(secs),線程暫停執行secs秒
調度器對象方法和屬性有:
方法或屬性 | 功能 |
---|---|
scheduler.enterabs(time, priority, action, argument=(), kwargs={}) | 安排一個新事件 |
scheduler.enter(delay, priority, action, argument=(), kwargs={}) | 安排延后 delay 時間單位的事件 |
scheduler.cancel(event) | 從隊列中刪除事件 |
scheduler.empty() | 判斷事件隊列是否為空 |
scheduler.run(blocking=True) | 運行所有預定事件 |
scheduler.queue | 按運行順序返回事件列表 |
總結?
原文鏈接:https://blog.csdn.net/weixin_44799217/article/details/127353545
相關推薦
- 2022-09-09 python?對excel交互工具的使用詳情_python
- 2022-11-10 詳解?PyTorch?Lightning模型部署到生產服務中_python
- 2022-12-24 C#如何優雅的對WinForm窗體應用程序進行權限控制_C#教程
- 2022-05-06 基于python分享極坐標下的幾類典型曲線_python
- 2022-08-23 在Asp.net?core中實現websocket通信_實用技巧
- 2022-04-03 Pandas搭配lambda組合使用詳解_python
- 2023-05-15 Pandas處理DataFrame稀疏數據及維度不匹配數據分析詳解_python
- 2022-12-12 C語言使用函數實現字符串部分復制問題_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支