I'm found the module multiprocessing_generator. I tried that module with the code below :
from multiprocessing_generator import ParallelGenerator
def my_generator():
yield (x*x for x in range(200))
with ParallelGenerator(my_generator(), max_lookahead=100) as g:
for elem in g:
print(elem)
Here is the error I got (I ran my code in the console, the python file is in my desktop) :
C:\Users\crd\Desktop>python test.py Traceback (most recent call last): File "test.py", line 69, in with ParallelGenerator(my_generator(), max_lookahead=100) as g: File "C:\Users\crd\AppData\Local\Programs\Python\Python37-32\lib\site-packages\multiprocessing_generator__init__.py", line 62, in enter self.process.start() File "C:\Users\crd\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\process.py", line 112, in start self._popen = self._Popen(self) File "C:\Users\crd\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\context.py", line 223, in _Popen return _default_context.get_context().Process._Popen(process_obj) File "C:\Users\crd\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\context.py", line 322, in _Popen return Popen(process_obj) File "C:\Users\crd\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\popen_spawn_win32.py", line 65, in init reduction.dump(process_obj, to_child) File "C:\Users\crd\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\reduction.py", line 60, in dump ForkingPickler(file, protocol).dump(obj) AttributeError: Can't pickle local object 'ParallelGenerator.init..wrapped'
C:\Users\crd\Desktop>Traceback (most recent call last): File "", line 1, in File "C:\Users\crd\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\spawn.py", line 99, in spawn_main new_handle = reduction.steal_handle(parent_pid, pipe_handle) File "C:\Users\crd\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\reduction.py", line 87, in steal_handle _winapi.DUPLICATE_SAME_ACCESS | _winapi.DUPLICATE_CLOSE_SOURCE) PermissionError: [WinError 5] Accès refusé
I cleaned up the code from the multiprocessing_generator module (I deleted every try/exeption, I converted the context-manager into a basic function). But with the code below I got the same error :
from queue import Empty
from multiprocessing import Process, Queue
def ParallelGeneratorLight():
queue = Queue()
def wrapped():
for item in (x*x for x in range(200)):
queue.put(item)
process = Process(target=wrapped)
process.start()
queue.get()
print(ParallelGeneratorLight())
What is wrong with that ?