Multiprocessing.process mit Spawn vs subprozess.popenPython

Python-Programme
Anonymous
 Multiprocessing.process mit Spawn vs subprozess.popen

Post by Anonymous »

Ich habe eine bindende C ++ Python -Bibliothek mit einer Klasse, die nur einmal pro Prozess initialisiert werden kann (unfaltlich, aufgrund des Legacy C ++ - Code).import multiprocessing
import pickle

class ProcessManager:
def __init__(self):
self._parent_conn, self._child_conn = multiprocessing.Pipe()
self._ready_event = multiprocessing.Event()
self._process = multiprocessing.Process(target=self.worker, args=(self._child_conn, self._ready_event))
self._process.start()
self._ready_event.wait()

def close(self):
"""
Closes the sub-process and the communication pipes
"""
if hasattr(self, '_process'):
if self._process.is_alive():
self._parent_conn.send('exit')
self._process.join()
self._parent_conn.close()
self._child_conn.close()

def __del__(self):
self.close()

@staticmethod
def worker(pipe, ready_event):
from pyLib import MyClass
obj = MyClass()
ready_event.set()
while True:
try:
msg = pipe.recv()
except EOFError:
break
if msg == 'exit':
break
method, args, kwargs = msg
try:
result = getattr(obj , method)(*args, **kwargs)
pipe.send(pickle.dumps(('ok', result)))
except Exception as e:
pipe.send(pickle.dumps(('error', e)))
< /code>
Dies hat funktioniert, bis ich eine neue Funktionalität für die gebundene Myclass getestet habe. /> Ich habe darüber gelesen und gesehen, dass multiprocessing.process standardmäßig in Unix Gabel verwendet, und so alle. So, die vom Elternteil geladen wurden, existieren auch im Speicherraum des Kindes - was dazu führt, Prozessumgebung und fragte sich, was der am meisten empfohlene Ansatz zur Lösung dieses Problems ist? Soweit ich weiß, ist dieser Ansatz für Nicht -Python -Programme besser geeignet? />
Thanks.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post