Periodic execution method of Python function

高洛峰
Release: 2017-02-25 13:22:57
Original
2090 people have browsed it

The examples in this article describe the implementation method of periodic execution of Python functions. Share it with everyone for your reference, the details are as follows:

You need to use python’s sched module:

#coding=utf-8
import time,sched,os
#初始化sched模块的scheduler类
#第一个参数是一个可以返回时间戳的函数,第二个参数可以在定时未到达之前阻塞。
s = sched.scheduler(time.time,time.sleep)
#被周期性调度触发的函数
def event_func():
  print "Current Time:",time.time()
#enter四个参数分别为:间隔事件、优先级(用于同时间到达的两个事件同时执行时定序)、被调用触发的函数,给他的参数(注意:一定要以tuple给如,如果只有一个参数就(xx,))
def perform(inc):
  s.enter(inc,0,perform,(inc,))
  event_func()
def mymain(inc=60):
  s.enter(0,0,perform,(inc,))
  s.run()
# if __name__ == "__main__":
  # mymain()
Copy after login

More related to the periodic execution method of Python functions Please pay attention to the PHP Chinese website for articles!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template