python - How to access same multiprocessing namespace from different modules -
i need able create shared object pyserial object in it. object created child process once after finding device list of locations. other processes use in later time.
python multiprocessing manager can't know changes objects embedded other objects. if create manager:
import multiprocessing mp manager=mp.manager() ns=manager.namespace()
i can share object between processes.
ns.obj = serialreader()
where
class serialreader(object): port = none def connect(self): #some code test connected device ... #end of code ser=serial.serial(device, etc) self.ser=ser #or self.saveport() #for future use def saveport(self): self.port = self.ser._port ns.port= self.ser._port
now run in child process:
p=process(target = ns.obj.connect) p.start()
and print results:
print ns.obj.port print ns.port
output:
none /dev/ttyacm0
i whant able use simple code like:
ns.obj.ser.write(), ns.obj.somemethod(arg) where
...inside serialreaders class... def somemethod(self, arg): if arg == condition: self.ser.write('some text %s' %arg)
but can't make refference ns.obj.ser because concidered not defined if run new process. same situation if reference self.ser in other methods inside conntrollers.py , try run them in new process.
edit: found way import namespace module:
from __main__ import ns
or sending ns init when creating object. problem still exist. ns.obj nonetype object because object still in process of creation. can't type ns.obj.ser= self.ser if try send ns serialreader()
ns.obj = serialreader(ns)
and try print inside serialreader_init_ get:
<namespaceproxy object, typeid 'namespace' @ 0xa21b70c; '__str__()' failed>
i can't add ser too..
Comments
Post a Comment