简体   繁体   English

Python 同步子类线程

[英]Python synchronize subclass threads

First of all, I'm new to threading in Python and actually pretty new to Python in general, so please excuse if the question is not well asked.首先,我是 Python 中的threading新手,实际上对于 Python 来说一般来说还是很新的,所以如果这个问题问得不好,请原谅。

I have 2 modules that each include one class.我有 2 个模块,每个模块包括一个 class。 These classes are both subclasses of threading.Thread .这些类都是threading.Thread的子类。 I start these two threads from a third module, main.py :我从第三个模块main.py启动这两个线程:

from controller import Controller
from server import Server
from ultrasonic import Ultrasonic

if __name__ == "__main__":
    controller = Controller()

    server = Server(controller)
    server.start()

    ultrasonic = Ultrasonic(controller)
    ultrasonic.start()

As you can see, both threads access another class in a different module, Controller .如您所见,两个线程都访问另一个模块 Controller 中的另一个Controller To make sure the two threads won't interfere while calling methods of Controller , I want to synchronize these threads.为了确保两个线程在调用Controller的方法时不会相互干扰,我想同步这些线程。 I saw, that there's the option of using threading.Lock , but I'm not sure how to implement this in my specific case, with different subclasses of threading.Thread in different modules.我看到,可以选择使用threading.Lock ,但我不确定如何在我的特定情况下使用不同模块中的不同子类threading.Thread来实现这一点。

I hope y'all can help me.我希望你们都可以帮助我。 Thank you!谢谢!

EDIT: The methods in the Controller class look like this:编辑: Controller class 中的方法如下所示:

def stop(self):
    GPIO.output(self.LEFT_PIN1, False)
    GPIO.output(self.LEFT_PIN2, False)
    GPIO.output(self.RIGHT_PIN1, False)
    GPIO.output(self.RIGHT_PIN2, False)

I thought it might lead to problems if the GPIO pins recieved contrary signals.我认为如果 GPIO 引脚接收到相反的信号可能会导致问题。 If that's not the case, then I don't need to synchronize the threads in the first place.如果不是这种情况,那么我不需要首先同步线程。

My guess is that your Controller class could create a Lock :我的猜测是您的 Controller class 可以创建一个Lock

from Threading import Lock

class Controller:
    lock = Lock()    # class level instance

    def stop(self):
        Controller.lock.acquire()
        GPIO.output(self.LEFT_PIN1, False)
        GPIO.output(self.LEFT_PIN2, False)
        GPIO.output(self.RIGHT_PIN1, False)
        GPIO.output(self.RIGHT_PIN2, False)
        Controller.lock.release()

This way the Controller class creates a single Lock when it is defined.这样 Controller class 在定义时会创建一个Lock

When any call is make to stop() , the current thread acquires the lock and releases it upon return.当对stop()进行任何调用时,当前线程acquires锁并在返回时释放它。 Any other thread which makes it to the acquire() call, blocks until the first thread releases it.任何其他进入acquire()调用的线程都会阻塞,直到第一个线程释放它。

Better is to use context management and the with statement:更好的是使用上下文管理和with语句:

    def stop(self):
        with Controller.lock:
            GPIO.output(self.LEFT_PIN1, False)
            GPIO.output(self.LEFT_PIN2, False)
            GPIO.output(self.RIGHT_PIN1, False)
            GPIO.output(self.RIGHT_PIN2, False)

Note that I've used a Lock at the class level since you are locking access to the GPIO pins of your processor and there will only be one set of those.请注意,我在 class 级别使用了Lock ,因为您正在锁定对处理器GPIO引脚的访问,并且只有一组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM