From a052603aa4ae97a5119647d32f7e8bc1872ee570 Mon Sep 17 00:00:00 2001 From: Adam Olech Date: Fri, 30 Apr 2021 00:40:30 +0200 Subject: Add concurrent_append.py --- concurrent_append.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 concurrent_append.py (limited to 'concurrent_append.py') diff --git a/concurrent_append.py b/concurrent_append.py new file mode 100644 index 0000000..8572e94 --- /dev/null +++ b/concurrent_append.py @@ -0,0 +1,24 @@ +# +# Shamelessly stolen from the following StackOverflow response: +# https://stackoverflow.com/a/56792928/6150271 +# + +import time +from concurrent.futures import ProcessPoolExecutor + +id_array = [*range(10)] + +def myfunc(id): + time.sleep(5) + if id % 2 == 0: + return id, id + else: + return id, id ** 2 + + +result = [] +with ProcessPoolExecutor() as executor: + for r in executor.map(myfunc, id_array): + result.append(r) + +print(result) -- cgit v1.2.1