Heim >Backend-Entwicklung >Python-Tutorial >Python定时执行之Timer用法示例

Python定时执行之Timer用法示例

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-06 11:17:441785Durchsuche

本文实例讲述了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' 

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

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

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn