Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

automatic multiprocessing

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


chengsoon.ong at inf

Jul 7, 2009, 8:08 AM

Post #1 of 2 (154 views)
Permalink
automatic multiprocessing

Hi all,

I'm trying to automate the use of multiprocessing when it is available. The
setting I have is quite simple, with a for loop where the operations inside are
independent of each other. Here's a bit of code. function_inputs is a list of
dictionaries, each of which match the signature of function_handle.

if multiprocessing_present:
# Passing keyword arguments to map still doesn't work
cpus = multiprocessing.Pool()
function_outputs = cpus.map(function_handle, function_inputs)
else:
function_outputs = []
for kwargs in function_inputs:
cur_out = function_handle(**kwargs)
function_outputs.append(cur_out)

Am I missing something here? I cannot seem to get map to pass on keyword arguments.

Thanks in advance,
Cheng Soon
--
http://mail.python.org/mailman/listinfo/python-list


sajmikins at gmail

Jul 7, 2009, 9:16 AM

Post #2 of 2 (140 views)
Permalink
Re: automatic multiprocessing [In reply to]

On Jul 7, 11:08 am, Cheng Soon Ong <chengsoon....@inf.ethz.ch> wrote:
> Hi all,
>
> I'm trying to automate the use of multiprocessing when it is available. The
> setting I have is quite simple, with a for loop where the operations inside are
> independent of each other. Here's a bit of code. function_inputs is a list of
> dictionaries, each of which match the signature of function_handle.
>
>      if multiprocessing_present:
>          # Passing keyword arguments to map still doesn't work
>          cpus = multiprocessing.Pool()
>          function_outputs = cpus.map(function_handle, function_inputs)
>      else:
>          function_outputs = []
>          for kwargs in function_inputs:
>              cur_out = function_handle(**kwargs)
>              function_outputs.append(cur_out)
>
> Am I missing something here? I cannot seem to get map to pass on keyword arguments.
>
> Thanks in advance,
> Cheng Soon

Pool.map() doesn't handle "**dict" keyword argument notation
automatically. You could use a wrapper function like so:

cpus = multiprocessing.Pool()
def f(kwargs):
return function_handle(**kwargs)
function_outputs = cpus.map(f, function_inputs)

(Note that f() could be defined outside the if statement if you're
going to use it often.)

HTH,
~Simon
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.