summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Olech <nddr89@gmail.com>2021-04-30 00:40:30 +0200
committerAdam Olech <nddr89@gmail.com>2021-04-30 00:40:30 +0200
commita052603aa4ae97a5119647d32f7e8bc1872ee570 (patch)
tree86221c1616d27a0dcdf264e56e0582411c8d02e4
parentb2899aeb4455719fe622abeb250ab5f5d8b3a739 (diff)
Add concurrent_append.py
-rw-r--r--concurrent_append.py24
1 files changed, 24 insertions, 0 deletions
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)