Passing functions as parameter (multiprocessing)
MRAB
python at mrabarnett.plus.com
Tue Nov 13 07:51:28 EST 2012
On 2012-11-13 12:19, Jean-Michel Pichavant wrote:
> 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.
>
I believe the problem is that 'subproc' calls 'func', which is rebound
on the each iteration.
More information about the Python-list
mailing list