Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: Passing functions as parameter (multiprocessing) (http://www.velocityreviews.com/forums/t954469-re-passing-functions-as-parameter-multiprocessing.html)

Peter Otten 11-13-2012 12:51 PM

Re: Passing functions as parameter (multiprocessing)
 
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))




All times are GMT. The time now is 01:20 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.