【发布时间】:2013-03-05 00:18:59
【问题描述】:
在下面的代码中,我创建了一个线程,它打开了一个名为 candump 的函数。 Candump 监控输入通道并在数据进入时将值返回到标准输出。
我想要做的是控制何时终止(即,在 cansend 之后的固定时间量)。查看文档后,似乎加入可能是正确的方法?
我不确定。有什么想法吗?
import threading
from subprocess import call, Popen,PIPE
import time
delay=1
class ThreadClass(threading.Thread):
def run(self):
start=time.time()
proc=Popen(["candump","can0"],stdout=PIPE)
while True:
line=proc.stdout.readline()
if line !='':
print line
t = ThreadClass()
t.start()
time.sleep(.1)
call(["cansend", "can0", "-i", "0x601", "0x40", "0xF6", "0x60", "0x01", "0x00", "0x00", "0x00", "0x00"])
time.sleep(0.01)
#right here is where I want to kill the ThreadClass thread
【问题讨论】:
-
这里有一个XY problem。您是在问如何终止线程,还是在问如何在子进程上设置超时,而您只是认为线程终止是这样做的方法?如果是前者,这是Is there any way to kill a Thread in Python? 的重复。如果是后者,那就不是。 (您可以终止一个进程,这很容易。)
标签: python subprocess