Skip to content

Instantly share code, notes, and snippets.

@thomasweng15
Created December 14, 2022 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasweng15/9597791919e03b663e7c6b0d73f8922e to your computer and use it in GitHub Desktop.
Save thomasweng15/9597791919e03b663e7c6b0d73f8922e to your computer and use it in GitHub Desktop.
Multiprocessing passing values back
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