简体   繁体   English

如何在python中将类作为进程运行

[英]how to run a class as a process in python

I tried to open several serial ports in python at the same time. 我试图同时在python中打开几个串行端口。 I think it makes sense that every port as a class process, then I can group the respective properties and methods within this. 我认为将每个端口作为一个类进程是有意义的,然后可以在其中将各自的属性和方法分组。 Ok, I thought multiprocessing could be fine, but I have struggled to get it to work. 好的,我认为多处理可能很好,但是我一直在努力使其工作。

  1. Why can't I initialize the serial port in the init . 为什么我不能在init初始化串行端口。

  2. When using super(serialManager, self) without the init the loop_starter is executed, but not as a process. 使用不带initsuper(serialManager, self) ,将执行loop_starter ,但不作为进程执行。

  3. super(serialManager, self).__init__(target=self.loop_starter,args=(serial_port, baudrate, timeout)) isn't executed, why? super(serialManager, self).__init__(target=self.loop_starter,args=(serial_port, baudrate, timeout))未执行,为什么?

how can I properly put all props in a class and process a method inside this class? 如何正确地将所有道具放在一个类中并在该类中处理一个方法?

regards 问候

from multiprocessing import Process
import serial
import time

class serialManager(Process):
    def __init__(self, serial_port, baudrate=57200, timeout=1):
        self.light = False
        self.ser = serial.Serial(serial_port, baudrate=baudrate, timeout=timeout)  #1
        #super(serialManager, self)  #2
        #self.loop_starter(serial_port, baudrate, timeout) #2
        super(serialManager, self).__init__(target=self.loop_starter,args=(serial_port, baudrate, timeout))  #3

    def loop_starter( self, serial_port, baudrate, timeout):  
        print("loop_iterator init")
        ser = serial.Serial(serial_port, baudrate=baudrate, timeout=timeout)
        self.loop(ser)

    def loop(self, ser):  
        self.light = not (self.light)
        values = bytearray([2, 82, 49, 4])
        ser.write(values)
        print("loop")
        time.sleep(2)

    #def run(self):
        #print('run')

def main():
    print("main")

if __name__ == "__main__":

    msm = serialManager("COM7")
    print ("inited")
    try:
        msm.start()
        print ("started")

        #while True:
            #main()
    except KeyboardInterrupt:
        print("caught in main")
    finally:
        msm.join()

        while True:
            main()
            time.sleep (1)

ok, I also tried this little script without success. 好的,我也尝试了这个小脚本,但没有成功。 why isnt the run executed? 为什么不执行运行?

from multiprocessing import Process

import time

class P(Process):
    def __init__(self):
        super(P, self).__init__()
    def run(self):
        print("run")
        #time.sleep(0.5)

def main():
    while True:
        print("main")
        time.sleep(2.5)

if __name__ == "__main__":
    p = P()
    p.start()
    p.join()
    main()

Your test script executes run() fine. 您的测试脚本执行run()很好。 Per the Process documentation : 根据流程文档

If a subclass overrides the constructor, it must make sure it invokes the base class constructor (Process. init ()) before doing anything else to the process. 如果子类覆盖了构造函数,则必须确保在对进程进行其他任何操作之前,先调用基类的构造函数( Process。init ())。

Your serialManager class is invoking super() at the end instead of the begining of __init__ 您的serialManager类在最后而不是__init__ serialManager调用super()

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

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