Home > Backend Development > Python Tutorial > Python定时执行之Timer用法示例

Python定时执行之Timer用法示例

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 11:17:44
Original
1778 people have browsed it

本文实例讲述了Python定时执行之Timer用法。分享给大家供大家参考。具体分析如下:

java中Timer的作用亦是如此。python中的线程提供了java线程功能的子集。

#!/usr/bin/env python 
from threading import Timer 
import time 
timer_interval=1 
def delayrun(): 
  print 'running' 
t=Timer(timer_interval,delayrun) 
t.start() 
while True: 
  time.sleep(0.1) 
  print 'main running' 

Copy after login

t是一个Timer对象。【估计内部是使用一个线程】delay一秒钟之后执行delayrun函数。

其中time.sleep函数是用来让主线程暂停一点时间再继续执行。

希望本文所述对大家的Python程序设计有所帮助。

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