Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Passing functions as parameter (multiprocessing)

Reply
Thread Tools

Re: Passing functions as parameter (multiprocessing)

 
 
MRAB
Guest
Posts: n/a
 
      11-13-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=subpro c, 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Passing functions as parameter (multiprocessing) Peter Otten Python 0 11-13-2012 04:18 PM
Re: Passing functions as parameter (multiprocessing) Oscar Benjamin Python 0 11-13-2012 03:28 PM
Re: Passing functions as parameter (multiprocessing) Peter Otten Python 0 11-13-2012 12:51 PM
Passing parameter to function not expecting parameter Mister B C Programming 8 08-26-2010 08:01 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57