如何在等待特定延迟后启动Python线程

Sou*_*ran 4 python multithreading python-2.7

我有功能检测(),timeralg()

在运行 timeralg() 函数时,我希望 detector() 在特定延迟后并行启动。目前我尝试这样

def timeralg(c1,c2,c3,c4):
    t=[4,4,4,6,6,20,24,28,32,36,40]#delay determining array
    for y in range(0,3):
        print 'y is ',y
        if((c1>=c2)and(c1>=c3)):
            print 'timer1 on for'
            x=t[c1]        
            print x
            c1=0

        GPIO.output(5,False)#Red1
        GPIO.output(13,True)#red2
        GPIO.output(12,True)#red3
        GPIO.output(7,True)#green1
        if(y==2):
            t = threading.Thread(detection())
            t.start()
            print 'processing strtd in from 1'

        time.sleep(x-3)
        GPIO.output(7,False)
        GPIO.output(3,True)#Yellow1
        time.sleep(3)
        GPIO.output(3,False)#Yellow1
        GPIO.output(5,True)#Red1
Run Code Online (Sandbox Code Playgroud)

与此不同的是,我希望“t”在我指定的特定延迟之后启动。

Dav*_*ips 5

您可以detection()按如下方式换行:

def delayed_detection():
    time.sleep(3)
    detection()
Run Code Online (Sandbox Code Playgroud)

然后开始你的线程:

t = threading.Thread(delayed_detection)
t.start()
Run Code Online (Sandbox Code Playgroud)

您没有延迟线程的产生,但您仍然detecton()在三秒后实现调用