python中schedule模块的使用
来源:互联网 发布:软件质量管理指南 编辑:程序博客网 时间:2024/04/30 19:19
python中schedule模块的使用
标签(空格分隔): python job管理 schedule
python中schedule模块的使用
由于需要用到一个使用python进行job管理的模块,找到了schedule模块,简单好用,在这里记录一下。
详细源码可以参考这里
安装方法
pip install schedule
使用方法
import scheduleimport timedef job(): print("I'm working...")schedule.every(10).minutes.do(job)schedule.every().hour.do(job)schedule.every().day.at("10:30").do(job)schedule.every().monday.do(job)schedule.every().wednesday.at("13:15").do(job)while True: schedule.run_pending() time.sleep(1)
运行该程序之后,可以定时的进行执行。除了代码中提到的方法之外,还有例如seconds等的一些方法,可以参考源码。
TIPS
注意,schedule方法是串行的,也就是说,如果各个任务之间时间不冲突,那是没问题的;如果时间有冲突的话,会串行的执行命令,例如以下代码
import scheduleimport timeimport threadingdef job(): print("I'm working... in job1 start") time.sleep(15) print("I'm working... in job1 end")def job2(): print("I'm working... in job2")schedule.every(10).seconds.do(job)schedule.every(10).seconds.do(job2)while True: schedule.run_pending() time.sleep(1)
输出如下:
I’m working… in job1 start
I’m working… in job1 end
I’m working… in job2
可以改成多线程的方式:
import scheduleimport timeimport threadingdef job(): print("I'm working... in job1 start") time.sleep(15) print("I'm working... in job1 end")def job2(): print("I'm working... in job2")def run_threaded(job_func): job_thread = threading.Thread(target=job_func) job_thread.start() schedule.every(10).seconds.do(run_threaded,job) schedule.every(10).seconds.do(run_threaded,job2)while True: schedule.run_pending() time.sleep(1)
有如下输出:
I’m working… in job1 start
I’m working… in job2
I’m working… in job1 start
I’m working… in job2
I’m working… in job1 end
可见在并行的执行。
如果想要对线程的数量有所控制,“If you want tighter control on the number of threads use a shared jobqueue and one or more worker threads”,则可以采用如下方法:
import Queueimport timeimport threadingimport scheduledef job(): print("I'm working")def worker_main(): while 1: job_func = jobqueue.get() job_func()jobqueue = Queue.Queue()schedule.every(10).seconds.do(jobqueue.put, job)schedule.every(10).seconds.do(jobqueue.put, job)schedule.every(10).seconds.do(jobqueue.put, job)schedule.every(10).seconds.do(jobqueue.put, job)schedule.every(10).seconds.do(jobqueue.put, job)worker_thread = threading.Thread(target=worker_main)worker_thread.start()while 1: schedule.run_pending() time.sleep(1)
1 0
- python中schedule模块的使用
- python的schedule 模块用法示例
- python的schedule 模块用法示例
- python 中模块的使用
- python中telnetlib模块的使用
- Python中ConfigParser模块的使用
- Python中subprocess模块的使用
- python中bisect模块的使用
- Python中subprocess模块的使用
- Python中subprocess模块的使用
- python中json模块的使用
- Python中argparse模块的使用
- Python中logging模块的使用
- Python中paramiko模块的使用
- Python中APScheduler模块的使用
- python中yaml模块的使用
- python中re模块的使用
- Python中re模块的使用
- 分析进程、线程的终止
- spring mvc post请求参数所放的位置
- Virtualbox虚拟机中安装ubuntu
- Akka/play(activator) 2.5.3 创建工程 1
- Android6.0的通讯录获取
- python中schedule模块的使用
- 最优化与随机梯度下降
- Block的内存管理,看这里就够了
- 《C++精英内参之程序员高效指南》-12-8影响效率的不良习惯之科学的休息方法
- 超好用的C# json转换器。类js动态使用!
- templates
- BZOJ 4518: [Sdoi2016]征途
- 第四章 碰撞模块
- 【Java】finalize()和垃圾回收机制