Message from discussion
Passing functions as parameter (multiprocessing)
Received: by 10.180.87.170 with SMTP id az10mr3726809wib.0.1353486999550;
Wed, 21 Nov 2012 00:36:39 -0800 (PST)
Path: ha8ni3765wib.1!nntp.google.com!feeder2.cambriumusenet.nl!feeder1.cambriumusenet.nl!feeder3.cambriumusenet.nl!feed.tweaknews.nl!216.40.29.245.MISMATCH!novia!border4.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!news.glorb.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path: <python-python-l...@m.gmane.org>
X-Original-To: python-l...@python.org
Delivered-To: python-l...@mail.python.org
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:p 60': 0.05;
'python': 0.09; "%s'": 0.09; '**kwargs)': 0.09; '**kwargs):':
0.09; 'func': 0.09; 'issue:': 0.09; 'received:80.91': 0.09;
'received:80.91.229': 0.09; 'received:gmane.org': 0.09;
'received:list': 0.09; 'subject:skip:m 10': 0.09; 'def': 0.10;
'finished': 0.15; 'passing': 0.15; 'binding,': 0.16; 'fix:': 0.16;
'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16;
'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16;
'wrote:': 0.17; 'parameters': 0.20; 'import': 0.21; 'header:User-
Agent:1': 0.26; "doesn't": 0.28; 'header:X-Complaints-To:1': 0.28;
'invoke': 0.29; 'parameters.': 0.29; "i'm": 0.29; 'function':
0.30; 'code': 0.31; 'print': 0.32; 'to:addr:python-list': 0.33;
'received:org': 0.36; 'created': 0.36; 'but': 0.36; 'problems':
0.36; 'subject: (': 0.36; 'possible': 0.37; 'late': 0.37;
'subject:: ': 0.38; 'to:addr:python.org': 0.39;
'header:Received:5': 0.40; 'skip:u 10': 0.60; 'matter': 0.61;
'aka': 0.91
X-Injected-Via-Gmane: http://gmane.org/
To: python-l...@python.org
From: Peter Otten <__pete...@web.de>
Subject: Re: Passing functions as parameter (multiprocessing)
Date: Tue, 13 Nov 2012 13:51:27 +0100
Organization: None
References: <425807738.1035823.1352809155090.JavaMail.root@sequans.com>
Mime-Version: 1.0
X-Gmane-NNTP-Posting-Host: p5084b7d9.dip.t-dialin.net
User-Agent: KNode/4.7.3
X-BeenThere: python-l...@python.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <http://mail.python.org/mailman/options/python-list>,
<mailto:python-list-requ...@python.org?subject=unsubscribe>
List-Archive: <http://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-l...@python.org>
List-Help: <mailto:python-list-requ...@python.org?subject=help>
List-Subscribe: <http://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-requ...@python.org?subject=subscribe>
Newsgroups: comp.lang.python
Message-ID: <mailman.3625.1352811106.27098.python-l...@python.org>
Lines: 44
NNTP-Posting-Host: 2001:888:2000:d::a6
X-Trace: 1352811106 news.xs4all.nl 6902 [2001:888:2000:d::a6]:47054
X-Complaints-To: ab...@xs4all.nl
Bytes: 3831
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 7Bit
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=subproc, 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_subproc(func),
args=parameters))