2017-08-31 12 views
1

私はmultiprocess.Process()を継承するクラスを作成しました。クラス属性にserial.Serial()オブジェクトを保持します。 self.loop()メソッドはシリアルポートから読み書きすることになっています。 self.loop()が呼び出されると、別のプロセスとして実行されることになっています。これは、これを書くように要求された人の要件です。しかし、私のコードは、奇妙なエラーを生成します。マルチプロセッシングとシリアルポートの混合

これは私のコードです:

from multiprocessing import Process 

import serial 
import time 


class MySerialManager(Process): 
    def __init__(self, serial_port, baudrate=115200, timeout=1): 
     super(MySerialManager, self).__init__(target=self.loop) 
     # As soon as you uncomment this, you'll get an error. 
     # self.ser = serial.Serial(serial_port, baudrate=baudrate, timeout=timeout) 

    def loop(self): 
     # Just some simple action for simplicity. 
     for i in range(3): 
      print("hi") 
      time.sleep(1) 


if __name__ == "__main__": 
    msm = MySerialManager("COM14") 
    try: 
     msm.start() 
    except KeyboardInterrupt: 
     print("caught in main") 
    finally: 
     msm.join() 

これはエラーです:

Traceback (most recent call last): 
    File "test.py", line 22, in <module> 
    msm.start() 
    File "C:\Python\Python36\lib\multiprocessing\process.py", line 105, in start 
    self._popen = self._Popen(self) 
    File "C:\Python\Python36\lib\multiprocessing\context.py", line 223, in _Popen 
    return _default_context.get_context().Process._Popen(process_obj) 
    File "C:\Python\Python36\lib\multiprocessing\context.py", line 322, in _Popen 
    return Popen(process_obj) 
    File "C:\Python\Python36\lib\multiprocessing\popen_spawn_win32.py", line 65, in __init__ 
    reduction.dump(process_obj, to_child) 
    File "C:\Python\Python36\lib\multiprocessing\reduction.py", line 60, in dump 
    ForkingPickler(file, protocol).dump(obj) 
ValueError: ctypes objects containing pointers cannot be pickled 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "test.py", line 26, in <module> 
    msm.join() 
    File "C:\Python\Python36\lib\multiprocessing\process.py", line 120, in join 
    assert self._popen is not None, 'can only join a started process' 
AssertionError: can only join a started process 
Traceback (most recent call last): 
    File "<string>", line 1, in <module> 
    File "C:\Python\Python36\lib\multiprocessing\spawn.py", line 105, in spawn_main 
    exitcode = _main(fd) 
    File "C:\Python\Python36\lib\multiprocessing\spawn.py", line 115, in _main 
    self = reduction.pickle.load(from_parent) 
EOFError: Ran out of input 

私は、クラスの外でシリアルポートオブジェクトを作成し、コンストラクタに渡す試してみました。さらに、私はmultiprocess.Process()を継承していない試みたが、代わりに置く:

try: 
    msm.proc.start() 
except KeyboardInterrupt: 
    print("caught in main") 
finally: 
    msm.proc.join() 

self.proc = Process(target=self.loop) 
クラスに

をメインブロックに。どちらも問題を解決しませんでした。

誰かが、マルチプロセッシングとシリアルポートの混在がうまくいかないと指摘しました。本当?もしそうなら、なぜこれが動かないのか私に説明してもらえますか?どんな助けでも大歓迎です! Windowsで一度作成されたシリアル・オブジェクトは、二つのプロセス(すなわち。親と子)の間で共有することができません

答えて

1

ので、子プロセスでシリアル・オブジェクトを作成し、他の関数に引数として

試しているの参照を渡しますこれは:

from multiprocessing import Process 

import serial 
import time 


class MySerialManager(Process): 
    def __init__(self, serial_port, baudrate=115200, timeout=1): 
     super(MySerialManager, self).__init__(target=self.loop_iterator,args=(serial_port, baudrate, timeout)) 
     # As soon as you uncomment this, you'll get an error. 
     # self.ser = serial.Serial(serial_port, baudrate=baudrate, timeout=timeout) 

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

    def loop(self,ser): 
     # Just some simple action for simplicity. 
     # you can use ser here 
     for i in range(3): 
      print("hi") 
      time.sleep(1) 


if __name__ == "__main__": 
    msm = MySerialManager("COM4") 
    try: 
     msm.start() 
    except KeyboardInterrupt: 
     print("caught in main") 
    finally: 
     msm.join() 
関連する問題