如何在raspberry pi 3上终止python中的线程?
这个问题相对简单,只是我找不到 通过谷歌搜索术语回答:如何在raspberry pi 3上终止python中的线程?,python,multithreading,Python,Multithreading,这个问题相对简单,只是我找不到 通过谷歌搜索术语回答: 如何在python中终止线程 如何在线程中使用键盘输入结束循环等 所以程序的格式是: import everything necessary def readingsomething(): DOING SOME WORK in a infinite while loop and sleep for 1 sec def readingsomeotherthing(): DOING SOME WORK in a in
import everything necessary
def readingsomething():
DOING SOME WORK in a infinite while loop and sleep for 1 sec
def readingsomeotherthing():
DOING SOME WORK in a infinite while loop and sleep for 2 sec
thread1 = thread.thread(target = readingsomething)
thread2 = thread.thread(target = readingsomeotherthing)
try:
thread1.start()
thread2.start()
thread1.join()
thread2.join()
except KeyboardInterrupt:
save a file and sys.exit()
所以当我运行程序时,除了我
按ctrl+c键,它不会按照键盘中断终止
我正在丢失收集的数据,因为我无法保存它们
如果您有任何建议和帮助,我们将不胜感激。您想做什么还不清楚。 你说的是循环,但我在你的代码中看不到循环 同样,这样编写,您将首先等待thread1停止,然后等待thread2停止,确保它是您想要的 在这些“join”调用中设置超时,否则将阻止侦听异常:
thread1.join()
变成
thread1.join(10)
您可能需要考虑它对您的代码所带来的更改。您试图做什么还不清楚。 你说的是循环,但我在你的代码中看不到循环 同样,这样编写,您将首先等待thread1停止,然后等待thread2停止,确保它是您想要的 在这些“join”调用中设置超时,否则将阻止侦听异常:
thread1.join()
变成
thread1.join(10)
您可能需要考虑它对代码造成的更改。您可以使用同步队列作为管道向线程发送值 您可以将同步队列用作向线程发送值的管道 工作Python 3示例:
from threading import Thread, Event
import time
def readingsomething(stop):
while not stop.isSet():
print('readingsomething() running')
time.sleep(1)
def readingsomeotherthing(stop):
while not stop.isSet():
print('readingsomeotherthing() running')
time.sleep(2)
if __name__ == '__main__':
stop = Event()
thread1 = Thread(target=readingsomething, args=(stop,))
thread2 = Thread(target=readingsomeotherthing, args=(stop,))
thread1.start()
thread2.start()
try:
thread1.join()
thread2.join()
except KeyboardInterrupt:
print('catched KeyboardInterrupt')
stop.set()
#save the file
print('EXIT __main__')
使用Python:3.4.2测试使用Python 3示例:
from threading import Thread, Event
import time
def readingsomething(stop):
while not stop.isSet():
print('readingsomething() running')
time.sleep(1)
def readingsomeotherthing(stop):
while not stop.isSet():
print('readingsomeotherthing() running')
time.sleep(2)
if __name__ == '__main__':
stop = Event()
thread1 = Thread(target=readingsomething, args=(stop,))
thread2 = Thread(target=readingsomeotherthing, args=(stop,))
thread1.start()
thread2.start()
try:
thread1.join()
thread2.join()
except KeyboardInterrupt:
print('catched KeyboardInterrupt')
stop.set()
#save the file
print('EXIT __main__')
使用Python测试:3.4.2您好,欢迎使用堆栈溢出。请确保在发布时使用,以便帮助您的人更容易阅读您的代码。请在执行代码时正确缩进代码。问题到底是什么?代码正在编译,没有错误并执行,但应该只按ctrl+c终止,但不会停止并继续执行。我认为这是由于函数中的sleep命令造成的问题。欢迎使用堆栈溢出。请确保在发布时使用,以便帮助您的人更容易阅读您的代码。请在执行代码时正确缩进代码。问题到底是什么?代码正在编译,没有错误并执行,但它应该只按ctrl+c终止,但它不会停止并继续执行。我认为这是一个问题,因为函数中有sleep命令。谢谢你的建议,我会尝试一下。循环在定义的函数中。谢谢你的建议,我会试试。循环在定义的函数中。感谢您的建议,我将尝试此操作。因为我使用的是python3,key=input('Press ctrl+c to terminate')变成key=eval(input('Press ctl+c to terminate'))但是我一直在c上遇到无效语法错误,我试图找到原因谢谢你,我能够以类似的方式解决问题。谢谢你的建议,我将尝试这个方法。因为我使用的是python3,key=input('Press ctrl+c to terminate')变成key=eval(input('Press ctl+c to terminate'))但是我一直在c上遇到无效的语法错误,我试图找到原因。谢谢你,我能够以类似的方式解决这个问题。