python开发--Python实现延时操做的几种方式

1. time.sleepredis

2. sched.schedulersegmentfault

3. threading.Timer异步

4. 借助其余程序函数

    celeryui

 redis延时队列 spa

在平常的开发中,每每会遇到这样的需求,须要某一个函数在一段时间以后才执行以达到某种特定的效果。此时,咱们就须要某种机制,使一个函数延后执行。接下来简单介绍一下两种实现此类效果的方法:
sched
    
import sched,time
def func(a):
  print time.time(),"Hello Sched!",a
print time.time()
s = sched.scheduler(time.time,time.sleep)
# 2为延后时间,1为优先级,func为函数名,("test1",)为函数参数
s.enter(2,1,func,("test1",))
s.enter(2,0,func,("test2",))
s.run()
print time.time()

输出结果以下:

    1519443179.4
    1519443181.4 Hello Sched! test2
    1519443181.4 Hello Sched! test1
    1519443181.4

从结果能够看出,函数果然延后了2s执行,而且test2比test1先执行,是由于一样是2s后执行,而且test2的优先级比test1高

timer
    
import threading,time
def func(a):
  print time.time(),"Hello Timer!",a
print time.time()
s = threading.Timer(2,func,("test",))
s.start()
print time.time()

输出结果以下:

    1519443055.69
    1519443055.69
    1519443057.69 Hello Timer! test

从结果能够看出,函数果然延后了2s执行。

从两种方式的输出结果能够看出,timer是异步执行的,并不卡住下面代码的执行,而sched会等到执行函数完成后才会往下执行。

 

一些参考:https://www.jianshu.com/p/944ae43e2662code

https://www.jianshu.com/p/a8c1458998aablog

http://www.javashuo.com/article/p-mqblqzqc-eq.html队列

https://tech.youzan.com/queuing_delay/开发

相关文章
相关标签/搜索