many of the tutorials on multiprocessing use don't seem to completely address why the technique below works for threading but not multiprocessing.
Why doesn't this work for multiprocessing, and what is the implementation for what I am trying to do? Thank you!
Threading implementation (works fine, makes sense to me):
from threading import Thread
from Queue import Queue
from time import sleep
"""threading functions"""
def producer_thread(n):
for x in range(10):
thread_q.put(n)
def consumer_thread():
while True:
item = thread_q.get()
print item
if __name__ == '__main__':
thread_q = Queue()
"""works fine"""
p_thread = Thread(target=producer_thread, args=(10,))
c_thread = Thread(target=consumer_thread)
c_thread.daemon=True
p_thread.start(); c_thread.start()
p_thread.join()
"""prevents c_thread daemon process from cancelling prematurely"""
sleep(.001)
Output:
10
10
10
10
10
10
10
10
10
10
Multiprocessing implementation (seems to be identical to threading but doesn't work at all):
from multiprocessing import Process, freeze_support
from Queue import Queue
"""multiprocessing functions"""
def producer_process(n):
for x in range(10):
process_q.put(n)
def consumer_process():
while True:
item = process_q.get()
print item
#
if __name__ == '__main__':
freeze_support()
process_q = Queue()
"""computer explodes"""
p_process = Process(target=producer_process, args=(10,))
c_process = Process(target=consumer_process)
c_process.daemon=True
p_process.start(); c_process.start()
p_process.join()
Output:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\J\Anaconda\lib\multiprocessing\forking.py", line 381, in main
self = load(from_parent)
File "C:\Users\J\Anaconda\lib\pickle.py", line 1378, in load
return Unpickler(file).load()
File "C:\Users\J\Anaconda\lib\pickle.py", line 858, in load
dispatch[key](self)
File "C:\Users\J\Anaconda\lib\pickle.py", line 1090, in load_global
klass = self.find_class(module, name)
File "C:\Users\J\Anaconda\lib\pickle.py", line 1126, in find_class
klass = getattr(mod, name)
AttributeError: 'module' object has no attribute 'get_successors'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\J\Anaconda\lib\multiprocessing\forking.py", line 381, in main
self = load(from_parent)
File "C:\Users\J\Anaconda\lib\pickle.py", line 1378, in load
return Unpickler(file).load()
File "C:\Users\J\Anaconda\lib\pickle.py", line 858, in load
dispatch[key](self)
File "C:\Users\J\Anaconda\lib\pickle.py", line 1090, in load_global
klass = self.find_class(module, name)
File "C:\Users\J\Anaconda\lib\pickle.py", line 1126, in find_class
klass = getattr(mod, name)
AttributeError: 'module' object has no attribute 'get_successors'
Process Process-33:
Traceback (most recent call last):
File "C:\Users\J\Anaconda\lib\multiprocessing\process.py", line 258, in _bootstrap
self.run()
File "C:\Users\J\Anaconda\lib\multiprocessing\process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\J\Documents\Python Scripts\producer_consumer_test.py", line 18, in consumer
item = q.get()
NameError: global name 'q' is not defined
Process Process-32:
Traceback (most recent call last):
File "C:\Users\J\Anaconda\lib\multiprocessing\process.py", line 258, in _bootstrap
self.run()
File "C:\Users\J\Anaconda\lib\multiprocessing\process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\J\Documents\Python Scripts\producer_consumer_test.py", line 14, in producer
q.put(n)
NameError: global name 'q' is not defined
Process Process-34:
Traceback (most recent call last):
File "C:\Users\J\Anaconda\lib\multiprocessing\process.py", line 258, in _bootstrap
self.run()
File "C:\Users\J\Anaconda\lib\multiprocessing\process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\J\Documents\Python Scripts\producer_consumer_test.py", line 14, in producer
q.put(n)
NameError: global name 'q' is not defined
Process Process-35:
Traceback (most recent call last):
File "C:\Users\J\Anaconda\lib\multiprocessing\process.py", line 258, in _bootstrap
self.run()
File "C:\Users\J\Anaconda\lib\multiprocessing\process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\J\Documents\Python Scripts\producer_consumer_test.py", line 18, in consumer
item = q.get()
NameError: global name 'q' is not defined
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\J\Anaconda\lib\multiprocessing\forking.py", line 381, in main
self = load(from_parent)
File "C:\Users\J\Anaconda\lib\pickle.py", line 1378, in load
return Unpickler(file).load()
File "C:\Users\J\Anaconda\lib\pickle.py", line 858, in load
dispatch[key](self)
File "C:\Users\J\Anaconda\lib\pickle.py", line 1090, in load_global
klass = self.find_class(module, name)
File "C:\Users\J\Anaconda\lib\pickle.py", line 1126, in find_class
klass = getattr(mod, name)
AttributeError: 'module' object has no attribute 'consumer'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\J\Anaconda\lib\multiprocessing\forking.py", line 381, in main
self = load(from_parent)
File "C:\Users\J\Anaconda\lib\pickle.py", line 1378, in load
return Unpickler(file).load()
File "C:\Users\J\Anaconda\lib\pickle.py", line 858, in load
dispatch[key](self)
File "C:\Users\J\Anaconda\lib\pickle.py", line 1090, in load_global
klass = self.find_class(module, name)
File "C:\Users\J\Anaconda\lib\pickle.py", line 1126, in find_class
klass = getattr(mod, name)
AttributeError: 'module' object has no attribute 'producer'