如何创建多个可以停止的倒计时计时器线程

时间:2019-06-19 09:11:31

标签: python-3.x

我正在对led灯进行编程,以便当汽车经过基于opencv视频处理的区域时点亮。每次有车进入区域时,如果另一辆车进入该区域,则我将向树莓派发送信号以打开LED并启动倒数计时器,如果倒数计时器被取消,则该倒数计时器将被取消。发送到树莓派以关闭LED。倒数计时器必须是非阻塞的,以便opencv视频处理需要继续。

当我运行代码时,LED会立即打开和关闭,而不会倒计时结束

我尝试了线程计时器和异步

import asyncio
from threading import timer
def CntDwn():
    timedout = true
    # send on signal to raspberry pi
    sendSignal(ON)
def Process():
   #if car enters a region
   # first i tried using thread timer
    t1 = Timer(10.0, countdown('up'))
    t1.start()
   # using asyncio
    task1 = loop.create_task(CntDwn('OFF', 3))
   # if another car enters the region
    if (not timedout):
       # for timer
        t1.cance1
       # for asyncio
        task1.cancel()
        t1 = Timer(10.0, countdown('up'))
        t1.start()
#if timed out
    elif(timedout):
        # send off signal to raspberry pi
        sendSignal(OFF)
       # restart the timer
        t1 = Timer(10.0, countdown('up'))
        t1.start()

当我运行代码时,led会先打开然后立即关闭而不会延迟

0 个答案:

没有答案