python定时器重复执行_Python threading 单线程 timer重复调用函数

项目中需要使用定时器,每次都使用构造器函数调用:

timer = threading.Timer(timerFlag, upload_position)

timer.start()

打印线程后发现,每次都会创建一个新的子线程,虽然活跃的线程只有一个,但是也是种资源浪费:

print("threading active = {} \n \n".format(threading.enumerate()))

#打印

threading active = [<_mainthread stopped>, ]

threading active = [<_mainthread stopped>, ]

threading active = [<_mainthread stopped>, ]

threading active = [<_mainthread stopped>, ]

threading active = [<_mainthread stopped>, ]

threading active = [<_mainthread stopped>, ]

threading active = [<_mainthread stopped>, ]

阅读源码和文档

class Timer(Thread):

"""Call a function after a specified number of seconds:

t = Timer(30.0, f, args=None, kwargs=None)

t.start()<

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值