等待特定的延迟后如何在python中启动线程 [英] How to start a thread in python after waiting for a specific delay
本文介绍了等待特定的延迟后如何在python中启动线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有功能detection(),timeralg()
i have functions detection(),timeralg()
在运行timeralg()函数时,我希望detection()在特定延迟后并行开始. 目前,我尝试过这样
while running the timeralg() function, i want detection() to start in parallel after a specific delay. currently i tried like this
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
与此不同,我不想在我指定的特定延迟后开始.
Unlike this i want 't' to start after a specific delay specified by me.
推荐答案
您可以如下包装detection()
:
def delayed_detection():
time.sleep(3)
detection()
然后使用以下内容启动线程:
Then start your thread with:
t = threading.Thread(delayed_detection)
t.start()
您并没有延迟线程的产生,但是您仍然可以在三秒钟后实现调用detecton()
You're not delaying the spawning of the thread, but you are still achieving calling detecton()
after three seconds
这篇关于等待特定的延迟后如何在python中启动线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文