Passing functions as parameter (multiprocessing)

Jean-Michel Pichavant jeanmichel at
Tue Nov 13 13:19:15 CET 2012


I'm having problems understanding an issue with passing function as parameters.

I'm sending some functions to the multiprocessing module (python 2.5 with the proper backport).
I'm iterating on a list of functions, however it seems that only the last function implementation is used for 
all the subprocesses.

Here's a code that triggers the issue:

import multiprocessing

def f1():
    print 'I am f1'
def f2(foo):
    print 'I am f2 %s' % foo

workers = [

for func, parameters in workers:
    # here it should be decorated, but for this example to be kept simple, the function is only wrapped, doing nothing special
    def subproc(*args, **kwargs):
        return func(*args, **kwargs)
    procs.append(multiprocessing.Process(target=subproc, args=parameters))

for proc in procs:
for proc in procs:

Here's the result:
> run
Process Process-1:
Traceback (most recent call last):
  File "/usr/lib/python2.5/site-packages/multiprocessing-", line 237, in _bootstrap
  File "/usr/lib/python2.5/site-packages/multiprocessing-", line 93, in run
    self._target(*self._args, **self._kwargs)
  File "", line 17, in subproc
    return func(*args, **kwargs)
TypeError: f2() takes exactly 1 argument (0 given)
I am f2 5

It looks like the first subprocess is called with f2 instead of f1.

Any idea ?



The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

More information about the Python-list mailing list