Python如何与两个不同的线程共享一个串口(A类,B类)

发布于 2024-09-29 03:48:59 字数 1165 浏览 5 评论 0原文

我有一个使用串行端口(唯一资源)的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

您的好友蓝忘机已上羡 2024-10-06 03:48:59

虽然您可以使用适当的锁定来共享串行端口,但我不建议这样做。我已经用 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:

  • Have a single class, in a single thread, manage the actual serial port communication, via a Queue object or two:
    • Stuff read from the port is placed into the queue
    • Commands to send to the port are placed into the queue and the "Serial thread" sends them
  • Have the other threads implement logic by placing things into the queue and taking things out

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.

べ繥欢鉨o。 2024-10-06 03:48:59

A类中添加一个threading.Lock()并使其在使用时获取锁:

def __init__(self,dev):
    self.lock = threading.Lock()

def read(self):
    self.lock.acquire()
    data = ? #whatever you need to do here to read
    self.lock.release()
    return data

def write(self, data):
    self.lock.acquire()
    #whatever you need to do to write
    self.lock.release()

Add a threading.Lock() to class A and make it acquire the lock when using it:

def __init__(self,dev):
    self.lock = threading.Lock()

def read(self):
    self.lock.acquire()
    data = ? #whatever you need to do here to read
    self.lock.release()
    return data

def write(self, data):
    self.lock.acquire()
    #whatever you need to do to write
    self.lock.release()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文