Here i my code:
from concurrent.futures import ProcessPoolExecutor
class MyProcessPoolExecutor(ProcessPoolExecutor):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._running_workers = 0
def submit(self, *args, **kwargs):
future = super().submit(*args, **kwargs)
self._running_workers += 1
future.add_done_callback(self._worker_is_done)
return future
def _worker_is_done(self, future):
self._running_workers -= 1
@property
def pool_usage(self):
return self._running_workers
my_list = [1, 2, 3, 4, 5]
i = 0
def foo(x):
global i
i += 1
if i <= 10:
print(x)
my_list.append(x)
if __name__ == "__main__":
with MyProcessPoolExecutor() as executor:
finished = False
while not finished:
while my_list:
executor.submit(foo, my_list.pop(0))
finished = (executor.pool_usage == 0 and not my_list)
Basicly what I try to achieve is to get the list "my_list" to be printes twice on multiple processes. Problem is my code stops after jut one "iteration" - i tried to add new while statement as shown above but still getting just "1,2,3,4,5" not "1,2,3,4,5,1,2,3,4,5" as I'd expect. Has anyone encountered this problem and can anyone help me?