Python多线程队列剩余“空" [英] Python Multithreaded Queue remaining "empty"
问题描述
我有一个正在构建的 python 应用程序,用于运行一些测试,这些测试涉及使用空调制解调器 USB 到 USB(目前在 PC 上使用模拟器)将串行数据从一个 USB 端口发送到另一个端口.我写了一个串行监听器如下:
I have a python application that is being built to run some tests that involves using a Null Modem USB-to-USB (Currently using an emulator on PC) to send serial data from one USB port to another. I have written a serial listener as follows:
import serial
import threading
from queue import Queue
class SerialPort(object):
def ___init__(self, timeout=None):
self.ser = serial.Serial(baud=_, stopbits=_, ... timeout=timeout)
self.out_q = Queue()
self.in_q = Queue()
self.THREAD = None
def setup(self, com):
self.ser.port = com
self.ser.open()
def run(self):
self.THREAD = threading.Thread(target=self.listen, args=(self.out_q, self.in_q,))
self.THREAD.start()
def listen(self, in_q, out_q):
while True:
if not in_q.empty():
# This code is never reached, even though it should be
message = in_q.get()
if message == 'DIE':
break
else:
self.ser.write(message)
def send_command(self, command):
self.in_q.put(command)
class GUI(object):
def __init__(self):
self.POWER = False
self.server_port = SerialPort(timeout=0.1)
self.client_port = SerialPort(timeout=0.1)
#etc etc Tkinter stuff and things
def on_power_button_click(self):
# Tkinter Button already made, know the button works as expected
self.POWER = not self.POWER
if self.POWER:
self.server_port.setup('COM5')
self.client_port.setup('COM6')
self.server_port.run()
self.client_port.run()
else:
self.server_port.send_command('DIE')
self.client_port.send_command('DIE')
time.sleep(0.3)
self.server_port.ser.close()
self.client_port.ser.close()
my_example_problem = GUI()
# Creates threads and 'turns on' the application
my_example_problem.on_power_button_click()
# Should Turn off the application but doesn't
my_example_problem.on_power_button_click()
一切正常,但每当它们关闭时,'DIE' 命令永远不会在 listen() 中注册,并且 in_q.empty() 被卡住为 False,我不知道为什么.我想知道这是否可能是范围问题,但是我有另一个应用程序,它是用完全相同的范围编写的,但只使用一个线程并且工作得很好,据我了解,Python 队列的工作方式类似于 C 队列指向起始内存块的指针,因此范围应该无关紧要.
Things turn on just fine, but whenever they are turned off, the 'DIE' command never gets registered in listen(), and in_q.empty() is stuck as being False, and I can't figure out why. I'm wondering if it might be a scope problem, however I have another application that is written with the exact same scopes but only uses one thread and works just fine, and from my understanding a Python Queue works similarly to a C queue with a pointer to the starting memory block so scope shouldn't matter.
推荐答案
问题是你的目标函数 listen
有签名 ..., in_q, out_q
接受 输入队列 首先,然后输出队列 - 但是Thread
(在run()
函数)以错误的参数顺序开始:
The problem is that your target function listen
has signature ..., in_q, out_q
accepting input queue first, then output queue - but the Thread
(within run()
function) is started with wrong order of arguments:
... , args=(self.out_q, self.in_q,)
在线程实例化时更改参数/队列顺序:
Change arguments/queues order on Thread instantiation:
self.THREAD = threading.Thread(target=self.listen, args=(self.in_q, self.out_q,))
这篇关于Python多线程队列剩余“空"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!