python - How to start processes with methods as targets in a class context? -


i trying start several processes in class context should share queue:

import multiprocessing import queue  class mymulti:     def __init__(self):         self.myq = queue.queue()      def printhello(self):         print("hello")         self.myq.put("hello")      def run(self):         _ in range(5):             p = multiprocessing.process(target=self.printhello)             p.start()  if __name__ == "__main__":     multiprocessing.freeze_support()     m = mymulti()     m.run()     # @ point queue being filled in 5 elements 

this crashes with

c:\python34\python.exe c:/users/yop/dev/getnessusscans/tests/testm.py traceback (most recent call last):   file "c:/users/yop/dev/getnessusscans/tests/testm.py", line 20, in <module>     m.run()   file "c:/users/yop/dev/getnessusscans/tests/testm.py", line 15, in run     p.start()   file "c:\python34\lib\multiprocessing\process.py", line 105, in start     self._popen = self._popen(self)   file "c:\python34\lib\multiprocessing\context.py", line 212, in _popen     return _default_context.get_context().process._popen(process_obj)   file "c:\python34\lib\multiprocessing\context.py", line 313, in _popen     return popen(process_obj)   file "c:\python34\lib\multiprocessing\popen_spawn_win32.py", line 66, in __init__     reduction.dump(process_obj, to_child)   file "c:\python34\lib\multiprocessing\reduction.py", line 59, in dump     forkingpickler(file, protocol).dump(obj) _pickle.picklingerror: can't pickle <class '_thread.lock'>: attribute lookup lock on _thread failed 

an answer similar question suggested have worker uppermost function, adapted case as

import multiprocessing import queue  def work(foo):     foo.printhello()  class mymulti:     def __init__(self):         self.myq = queue.queue()      def printhello(self):         print("hello")         self.myq.put("hello")      def run(self):         _ in range(5):             p = multiprocessing.process(target=work, args=(self,))             p.start()  if __name__ == "__main__":     multiprocessing.freeze_support()     m = mymulti()     m.run()     # @ point queue being filled in 5 elements 

this crashes same way, though.

is there way start processes methods targets?

i should have used self.myq = multiprocessing.queue() instead of queue.queue().

multiprocessing.queue() is, in addition of queue.queue(), process safe.

i leave question unanswered possibly comment if whole approach wrong.


Comments