Re: Passing functions as parameter (multiprocessing)

Discussion in 'Python' started by MRAB, Nov 13, 2012.

  1. MRAB

    MRAB Guest

    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.
    MRAB, Nov 13, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Metalone
    Replies:
    0
    Views:
    344
    Metalone
    Jan 6, 2010
  2. Mister B
    Replies:
    8
    Views:
    550
    Nick Keighley
    Aug 26, 2010
  3. Peter Otten
    Replies:
    0
    Views:
    154
    Peter Otten
    Nov 13, 2012
  4. Oscar Benjamin
    Replies:
    0
    Views:
    155
    Oscar Benjamin
    Nov 13, 2012
  5. Peter Otten
    Replies:
    0
    Views:
    186
    Peter Otten
    Nov 13, 2012
Loading...

Share This Page