Python如何与两个不同的线程共享一个串口(A类,B类)
我有一个使用串行端口(唯一资源)的Python进程,该进程使用类A的实例进行管理。存在使用类B和C的实例初始化的两个不同的线程,它们不断地使用串行端口资源对象已经创建。
import threading
Class A(threading.Thread):
#Zigbee serial port handler
def __init__(self,dev):
#something here that initialize serial port
def run():
while True:
#listening serial interface
def pack(self):
#something
def checksum(self):
#something
def write(self):
#something
Class B(threading.Thread):
def __init__(self,SerialPortHandler):
self.serialporthandler=SerialPortHandler
def run(self)
while True:
#something that uses self.serialporthandler
Class C(threading.Thread):
def __init__(self,SerialPortHandler):
self.serialporthandler=SerialPortHandler
def run(self)
while True:
#something that uses self.serialporthandler
def main():
a=A('/dev/ttyUSB1')
b=B(a)
b.start()
c=C(a)
c.start()
if __name__=='main':
while True:
main()
问题是两个线程都试图同时访问串行资源。我可以使用同一类 A 的多个实例,在敏感部分附加 Lock.acquire() 和 Lock.release() 。
你们中的一些人能给我指出正确的方法吗?
先感谢您。
I have a single Python process which is using a serial port (unique resource) which is managed using an instance of a class A. There exists two different threads initialized using instances of classes B and C, which are constantly using the serial port resource through the objected already created.
import threading
Class A(threading.Thread):
#Zigbee serial port handler
def __init__(self,dev):
#something here that initialize serial port
def run():
while True:
#listening serial interface
def pack(self):
#something
def checksum(self):
#something
def write(self):
#something
Class B(threading.Thread):
def __init__(self,SerialPortHandler):
self.serialporthandler=SerialPortHandler
def run(self)
while True:
#something that uses self.serialporthandler
Class C(threading.Thread):
def __init__(self,SerialPortHandler):
self.serialporthandler=SerialPortHandler
def run(self)
while True:
#something that uses self.serialporthandler
def main():
a=A('/dev/ttyUSB1')
b=B(a)
b.start()
c=C(a)
c.start()
if __name__=='main':
while True:
main()
The problem is that both threads are trying to access the serial resource at the same time. I could use several instances of the same class A, attaching Lock.acquire() and Lock.release() in the sensitive parts.
Could some of you point me to the right way?
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然您可以使用适当的锁定来共享串行端口,但我不建议这样做。我已经用 Python 编写了几个在串行端口上进行通信的多线程应用程序,根据我的经验,以下方法更好:
管理实际的串行端口通信队列对象或两个:- 从端口读取的内容被放入队列
- 发送到端口的命令被放入队列中,然后“串行线程”发送它们
使用
Queue
对象将极大地简化您的代码并使其更加健壮。这种方法在设计方面为您带来了很多可能性。例如,您可以向串行线程管理器注册事件(回调),并让它在发生有趣的事件时(以同步方式)调用它们,等等。
While you could share the serial port using appropriate locking, I wouldn't recommend it. I've written several multi-threaded applications that communicate on the serial port in Python, and in my experience the following approach is better:
Queue
object or two:Using
Queue
objects will greatly simplify your code and make it more robust.This approach opens a lot of possibilities for you in terms of design. You can, for example, register events (callbacks) with the serial thread manager and have it call them (in a synchronized way) when interesting events occur, etc.
在
A类
中添加一个threading.Lock()
并使其在使用时获取锁:Add a
threading.Lock()
toclass A
and make it acquire the lock when using it: