相关疑难解决方法(0)

每隔x分钟执行一次函数:sched或threading.Timer?

我需要每隔x分钟编写一次给定方法的执行.

我找到了两种方法:第一种是使用sched模块,第二种是使用模块Threading.Timer.

第一种方法:

import sched, time
s = sched.scheduler(time.time, time.sleep)
def do_something(sc): 
    print "Doing stuff..."
    # do your stuff
    sc.enter(60, 1, do_something, (sc,))

s.enter(60, 1, do_something, (s,))
s.run()
Run Code Online (Sandbox Code Playgroud)

第二个:

import threading

def do_something(sc): 
    print "Doing stuff..."
    # do your stuff
   t = threading.Timer(0.5,do_something).start()

do_something(sc)
Run Code Online (Sandbox Code Playgroud)

有什么区别,如果有一个比另一个好,哪一个?

python timer python-multithreading

8
推荐指数
1
解决办法
5640
查看次数

标签 统计

python ×1

python-multithreading ×1

timer ×1