Passing functions as parameter (multiprocessing)

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


Fellows,

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 = [
        (f1,tuple()),
        (f2,(5,)),
        ]

procs=[]
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:
    proc.start()
for proc in procs:
    proc.join()


Here's the result:
> run test.py
Process Process-1:
Traceback (most recent call last):
  File "/usr/lib/python2.5/site-packages/multiprocessing-2.6.2.1-py2.5-linux-i686.egg/multiprocessing/process.py", line 237, in _bootstrap
    self.run()
  File "/usr/lib/python2.5/site-packages/multiprocessing-2.6.2.1-py2.5-linux-i686.egg/multiprocessing/process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
  File "test.py", 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 ?

JM


-- IMPORTANT NOTICE: 

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