在 python 中侦听线程事件
Listening for a threading Event in python
初次使用 SO 的用户,请原谅任何礼仪错误。我正在尝试在 python 中实现多线程程序,但遇到了麻烦。这无疑是由于对线程的实现方式缺乏了解,但希望您能帮我弄清楚。
我有一个基本程序,可以持续侦听串行端口上的消息,然后可以 print/save/process/etc 它们,工作正常。它基本上是这样的:
import serial
def main():
usb = serial.Serial('/dev/cu.usbserial-A603UBRB', 57600) #open serial w\ baud rate
while True:
line = usb.readline()
print(line)
然而我想做的是不断地监听串行端口上的消息,但不一定对它们做任何事情。这应该 运行 在后台,同时在前台我想有某种界面,用户可以命令程序 read/use/save 这些数据一段时间然后再次停止。
所以我创建了以下代码:
import time
import serial
import threading
# this runs in the background constantly, reading the serial bus input
class serial_listener(threading.Thread):
def __init__(self, line, event):
super(serial_listener, self).__init__()
self.event = threading.Event()
self.line = ''
self.usb = serial.Serial('/dev/cu.usbserial-A603UBRB', 57600)
def run(self):
while True:
self.line = self.usb.readline()
self.event.set()
self.event.clear()
time.sleep(0.01)
# this lets the user command the software to record several values from serial
class record_data(threading.Thread):
def __init__(self):
super(record_data, self).__init__()
self.line = ''
self.event = threading.Event()
self.ser = serial_listener(self.line,self.event)
self.ser.start() #run thread
def run(self):
while(True):
user_input = raw_input('Record data: ')
if user_input == 'r':
event_counter = 0
while(event_counter < 16):
self.event.wait()
print(self.line)
event_counter += 1
# this is going to be the mother function
def main():
dat = record_data()
dat.start()
# this makes the code behave like C code.
if __name__ == '__main__':
main()
它编译并 运行s,但是当我通过在 CLI 中键入 r 命令程序进行记录时,没有任何反应。它似乎没有收到任何事件。
任何线索如何使这项工作?解决办法也很好,唯一的问题是我不能一直打开和关闭串行接口,它必须一直保持打开状态,否则设备会停止工作,直到un/replugged。
我建议不要使用多线程,而是使用多进程。当你使用线程时,你必须考虑全局解释器锁。所以你要么听事件,要么在你的主线程中做一些事情。两者同时无效。
当使用多个进程时,我会使用一个队列来转发来自您想要处理的看门狗的事件。或者您可以编写自己的事件处理程序。 Here 您可以找到多进程事件处理程序的示例
初次使用 SO 的用户,请原谅任何礼仪错误。我正在尝试在 python 中实现多线程程序,但遇到了麻烦。这无疑是由于对线程的实现方式缺乏了解,但希望您能帮我弄清楚。
我有一个基本程序,可以持续侦听串行端口上的消息,然后可以 print/save/process/etc 它们,工作正常。它基本上是这样的:
import serial
def main():
usb = serial.Serial('/dev/cu.usbserial-A603UBRB', 57600) #open serial w\ baud rate
while True:
line = usb.readline()
print(line)
然而我想做的是不断地监听串行端口上的消息,但不一定对它们做任何事情。这应该 运行 在后台,同时在前台我想有某种界面,用户可以命令程序 read/use/save 这些数据一段时间然后再次停止。
所以我创建了以下代码:
import time
import serial
import threading
# this runs in the background constantly, reading the serial bus input
class serial_listener(threading.Thread):
def __init__(self, line, event):
super(serial_listener, self).__init__()
self.event = threading.Event()
self.line = ''
self.usb = serial.Serial('/dev/cu.usbserial-A603UBRB', 57600)
def run(self):
while True:
self.line = self.usb.readline()
self.event.set()
self.event.clear()
time.sleep(0.01)
# this lets the user command the software to record several values from serial
class record_data(threading.Thread):
def __init__(self):
super(record_data, self).__init__()
self.line = ''
self.event = threading.Event()
self.ser = serial_listener(self.line,self.event)
self.ser.start() #run thread
def run(self):
while(True):
user_input = raw_input('Record data: ')
if user_input == 'r':
event_counter = 0
while(event_counter < 16):
self.event.wait()
print(self.line)
event_counter += 1
# this is going to be the mother function
def main():
dat = record_data()
dat.start()
# this makes the code behave like C code.
if __name__ == '__main__':
main()
它编译并 运行s,但是当我通过在 CLI 中键入 r 命令程序进行记录时,没有任何反应。它似乎没有收到任何事件。
任何线索如何使这项工作?解决办法也很好,唯一的问题是我不能一直打开和关闭串行接口,它必须一直保持打开状态,否则设备会停止工作,直到un/replugged。
我建议不要使用多线程,而是使用多进程。当你使用线程时,你必须考虑全局解释器锁。所以你要么听事件,要么在你的主线程中做一些事情。两者同时无效。
当使用多个进程时,我会使用一个队列来转发来自您想要处理的看门狗的事件。或者您可以编写自己的事件处理程序。 Here 您可以找到多进程事件处理程序的示例