I am trying to turn my executor.submit
function call into a blocking function so that it waits until the ProcessPoolExecutor pool has available workers.
In other words, initially it should print 1,2, then wait for 5 seconds, print 3,4, etc.
How can I achieve this?
import concurrent.futures
import multiprocessing
import time
def wait_f():
time.sleep(5)
return 1
if __name__ == '__main__':
multiprocessing.freeze_support()
global_results = []
with concurrent.futures.ProcessPoolExecutor(max_workers=2) as executor:
futures = []
for j in range(10):
future = executor.submit(wait_f)
futures.append(future)
print(j)
for future in concurrent.futures.as_completed(futures):
result = future.result()
global_results.append(result)