python:定时任务模块schedule
1.安装 pip install schedule2.官网使用demo <span style="color: #0000ff;">def<span style="color: #000000;"> job():<span style="color: #0000ff;">print(<span style="color: #800000;">"<span style="color: #800000;">I'm working...<span style="color: #800000;">"<span style="color: #000000;">) schedule.every(10<span style="color: #000000;">).minutes.do(job) <span style="color: #0000ff;">while<span style="color: #000000;"> True: 3.拓展: 1.并行执行任务 (1)默认情况下,schedule按顺序执行所有作业。这背后的原因是很难找到一个让每个人都开心的并行执行模型 <span style="color: #0000ff;">def<span style="color: #000000;"> job():<span style="color: #0000ff;">print(<span style="color: #800000;">"<span style="color: #800000;">I'm running on thread %s<span style="color: #800000;">" %<span style="color: #000000;"> threading.current_thread()) <span style="color: #0000ff;">def<span style="color: #000000;"> run_threaded(job_func): schedule.every(10<span style="color: #000000;">).seconds.do(run_threaded,job) <span style="color: #0000ff;">while 1<span style="color: #000000;">: (2)如果需要控制线程数,就需要用queue <span style="color: #0000ff;">def<span style="color: #000000;"> job():<span style="color: #0000ff;">print(<span style="color: #800000;">"<span style="color: #800000;">I'm working<span style="color: #800000;">"<span style="color: #000000;">) <span style="color: #0000ff;">def<span style="color: #000000;"> worker_main(): jobqueue =<span style="color: #000000;"> Queue.Queue() schedule.every(10<span style="color: #000000;">).seconds.do(jobqueue.put,job) worker_thread = threading.Thread(target=<span style="color: #000000;">worker_main) <span style="color: #0000ff;">while 1<span style="color: #000000;">: (3)抛出异常 <span style="color: #0000ff;">def catch_exceptions(cancel_on_failure=<span style="color: #000000;">False):<span style="color: #0000ff;">def<span style="color: #000000;"> catch_exceptions_decorator(job_func): @functools.wraps(job_func) <span style="color: #0000ff;">def wrapper(*args,*<span style="color: #000000;">kwargs): <span style="color: #0000ff;">try<span style="color: #000000;">: <span style="color: #0000ff;">return job_func(args,**<span style="color: #000000;">kwargs) <span style="color: #0000ff;">except<span style="color: #000000;">: <span style="color: #0000ff;">import<span style="color: #000000;"> traceback <span style="color: #0000ff;">print<span style="color: #000000;">(traceback.format_exc()) <span style="color: #0000ff;">if<span style="color: #000000;"> cancel_on_failure: <span style="color: #0000ff;">return<span style="color: #000000;"> schedule.CancelJob <span style="color: #0000ff;">return<span style="color: #000000;"> wrapper <span style="color: #0000ff;">return<span style="color: #000000;"> catch_exceptions_decorator @catch_exceptions(cancel_on_failure=<span style="color: #000000;">True) schedule.every(5).minutes.do(bad_task) (4)只运行一次 schedule.every().day.at(<span style="color: #800000;">'<span style="color: #800000;">22:30<span style="color: #800000;">').do(job_that_executes_once)(5)一次取消多个任务 (schedule.every().day.do(greet,<span style="color: #800000;">'<span style="color: #800000;">Andrea<span style="color: #800000;">').tag(<span style="color: #800000;">'<span style="color: #800000;">daily-tasks<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">friend<span style="color: #800000;">'<span style="color: #000000;">)schedule.every().hour.do(greet,<span style="color: #800000;">'<span style="color: #800000;">John<span style="color: #800000;">').tag(<span style="color: #800000;">'<span style="color: #800000;">hourly-tasks<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">Monica<span style="color: #800000;">').tag(<span style="color: #800000;">'<span style="color: #800000;">hourly-tasks<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">customer<span style="color: #800000;">'<span style="color: #000000;">) schedule.every().day.do(greet,<span style="color: #800000;">'<span style="color: #800000;">Derek<span style="color: #800000;">').tag(<span style="color: #800000;">'<span style="color: #800000;">daily-tasks<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">guest<span style="color: #800000;">'<span style="color: #000000;">) schedule.clear(<span style="color: #800000;">'<span style="color: #800000;">daily-tasks<span style="color: #800000;">') (6)在任务中加入日志功能 <span style="color: #0000ff;">import<span style="color: #000000;"> schedule<span style="color: #008000;">#<span style="color: #008000;"> This decorator can be applied to (编辑:莱芜站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 是否可以在Python中模拟Scala的特征?
- 【Python】python进程池:multiprocessing.pool
- python – 将日期列和时间列合并到datetime列
- Django迁移是否应该在源代码控制中生效?
- python – 使用Jinja2模板中的DateTimeFields显示本地时间
- python – 使用Tensorflow中的多层感知器模型预测文本标签
- Python:如何在字符串中剪切超过2个相等字符的序列
- Python 类继承
- django – MongoEngine _types和_cls字段
- dict.viewkeys()返回的数据类型是什么? [python 2.7]