Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33234

Passing functions as parameter (multiprocessing)

Return-Path <prvs=65792cec0=jeanmichel@sequans.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.013
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'argument': 0.04; '(python': 0.05; "%s'": 0.09; '**kwargs)': 0.09; '**kwargs):': 0.09; 'issue:': 0.09; 'subject:skip:m 10': 0.09; 'typeerror:': 0.09; 'def': 0.10; 'passing': 0.15; 'to:name:python-list': 0.15; 'given)': 0.16; 'iterating': 0.16; 'proc': 0.16; 'result:': 0.16; 'skip:" 100': 0.16; 'subprocess': 0.16; 'module': 0.19; 'parameters': 0.20; 'import': 0.21; 'example': 0.23; 'seems': 0.23; 'idea': 0.24; 'looks': 0.26; '(most': 0.27; 'run': 0.28; 'parameters.': 0.29; "i'm": 0.29; 'function': 0.30; 'code': 0.31; 'file': 0.32; 'print': 0.32; 'traceback': 0.33; 'to:addr:python- list': 0.33; 'list': 0.35; 'doing': 0.35; 'but': 0.36; 'should': 0.36; 'thank': 0.36; 'problems': 0.36; 'subject: (': 0.36; 'store': 0.38; 'some': 0.38; 'nothing': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'takes': 0.39; 'called': 0.39; 'skip:u 10': 0.60; 'you.': 0.61; 'received:194': 0.61; 'first': 0.61; 'information': 0.63; 'here': 0.65; 'person,': 0.65; 'disclose': 0.69; 'notice:': 0.71; 'privileged.': 0.72; 'special': 0.73; '_bootstrap': 0.84; 'self.run()': 0.84; 'fellows,': 0.91; 'medium.': 0.91
X-IronPort-AV E=Sophos;i="4.80,766,1344204000"; d="scan'208";a="891336"
X-Virus-Scanned amavisd-new at zimbra.sequans.com
Date Tue, 13 Nov 2012 13:19:15 +0100 (CET)
From Jean-Michel Pichavant <jeanmichel@sequans.com>
To python-list <python-list@python.org>
Subject Passing functions as parameter (multiprocessing)
MIME-Version 1.0
X-Mailer Zimbra 7.2.0_GA_2669 (ZimbraWebClient - GC7 (Linux)/7.2.0_GA_2669)
Content-Type text/plain; charset="utf-8"
Content-Transfer-Encoding base64
X-BeenThere python-list@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-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3622.1352809199.27098.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352809199 news.xs4all.nl 6937 [2001:888:2000:d::a6]:37035
X-Complaints-To abuse@xs4all.nl
Path csiph.com!usenet.pasdenom.info!news.stben.net!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!usenetcore.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Xref csiph.com comp.lang.python:33234

Show key headers only | View raw


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.

Any idea ?

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Passing functions as parameter (multiprocessing) Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-11-13 13:19 +0100

csiph-web