blob: 8572e94bc73b1e3fec5eff8a7ec87850e50e2bd4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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)
|