Created
December 14, 2022 20:09
-
-
Save thomasweng15/9597791919e03b663e7c6b0d73f8922e to your computer and use it in GitHub Desktop.
Multiprocessing passing values back
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import multiprocessing as mp | |
def process(arguments, res_q): | |
(i,) = arguments | |
res_q.put([i]) | |
max_processes = 2 | |
queue = mp.Queue() | |
res_q = mp.Queue() # result queue | |
for idx in [1, 2, 3]: | |
queue.put([idx]) | |
if len(mp.active_children()) < max_processes: | |
p = mp.Process(target=process, args=(queue.get(), res_q)) | |
p.start() | |
# Checking if some items in the queue are unprocessed | |
while not queue.empty(): | |
if len(mp.active_children()) < max_processes: | |
p = mp.Process(target=process, args=(queue.get(), res_q)) | |
p.start() | |
p.join() | |
# Save results | |
while not res_q.empty(): | |
item = res_q.get() | |
print(item) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment