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)

 
 
Peter Otten
Guest
Posts: n/a
 
      11-13-2012
Jean-Michel Pichavant wrote:

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


> 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:


> def subproc(*args, **kwargs):
> return func(*args, **kwargs)
> procs.append(multiprocessing.Process(target=subpro c, args=parameters))


Python has late binding, and when the loop has finished the name func is
bound to f2. You have created multiple subproc functions, but that doesn't
matter as they all invoke func aka f2.

A possible fix:

def make_subproc(func):
def subproc(*args, **kwargs):
return func(*args, **kwargs)
return subproc

procs=[]
for func, parameters in workers:
procs.append(multiprocessing.Process(target=make_s ubproc(func),
args=parameters))


 
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) MRAB 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