Created
October 9, 2019 09:15
-
-
Save anielsen001/3e7e4dbe19b70058db030b5e1dad34d1 to your computer and use it in GitHub Desktop.
python multiprocessing example with return values
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
#!/usr/bin/env python3 | |
import multiprocessing as mp | |
SENTINEL = None | |
dat = range(10) | |
rets = [] | |
def function( *args ): | |
a = args[0] | |
return a | |
def worker( qIn, qOut ): | |
for f in iter( qIn.get, SENTINEL ): | |
res = function( f ) | |
qOut.put( res ) | |
qIn = mp.Queue() # to hold the inputs | |
qOut = mp.Queue() # to hold the outputs | |
procs = [ mp.Process( target = worker, args = [qIn, qOut] ) for i in range( mp.cpu_count() ) ] | |
for p in procs: | |
p.start() | |
for d in dat: | |
qIn.put( d ) | |
for p in procs: | |
qIn.put( SENTINEL ) | |
for p in procs: | |
p.join() | |
# put qOut back into a list | |
qlist = [] | |
while qOut.qsize() > 0: | |
qlist.append( qOut.get() ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment