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


Groups > comp.lang.python > #20828

Re: multiprocessing, what am I doing wrong?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!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@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'args': 0.05; 'sys': 0.05; 'imports': 0.07; 'run,': 0.07; 'script,': 0.07; 'python': 0.08; ':-(': 0.09; '__name__': 0.09; 'block.': 0.09; 'from:addr:python': 0.09; 'imported.': 0.09; 'def': 0.13; "'__main__':": 0.16; "'a'": 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'module:': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'reply-to:addr:python-list': 0.16; 'run(self):': 0.16; 'subject:wrong': 0.16; 'worker,': 0.16; 'wrote:': 0.18; 'header:In-Reply-To:1': 0.22; 'etc,': 0.23; 'here?': 0.23; 'skip:m 30': 0.24; 'starts': 0.24; 'happening': 0.24; 'process,': 0.25; 'module': 0.26; 'function': 0.27; 'import': 0.27; "i'm": 0.28; 'script': 0.28; 'forgot': 0.29; 'script.': 0.29; 'class': 0.29; 'print': 0.29; 'imported': 0.30; 'processes.': 0.30; 'queue': 0.30; 'creates': 0.31; 'subject:?': 0.31; 'does': 0.32; 'break': 0.32; "won't": 0.33; 'header:User- Agent:1': 0.33; 'file': 0.34; 'eric': 0.34; 'normally': 0.34; 'received:84': 0.34; 'skip:# 10': 0.34; 'anything': 0.34; 'reply- to:addr:python.org': 0.34; 'try:': 0.34; 'to:addr:python-list': 0.35; 'subject:skip:m 10': 0.37; 'run': 0.37; 'skip:_ 10': 0.38; 'doing': 0.38; 'except': 0.39; 'being': 0.40; 'to:addr:python.org': 0.40; 'your': 0.61; 'leading': 0.62; 'inbox': 0.68; 'header:Reply-To:1': 0.70; 'reply-to:no real name:2**0': 0.72; "'1'": 0.84; "'2'": 0.84; "'e'": 0.84; 'freeze': 0.84; 'workers,': 0.84; 'inbox,': 0.91
X-CM-Score 0.00
X-CNFS-Analysis v=2.0 cv=ZZifx7pA c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=qePWxsfFzlIA:10 a=FVV0ur5EMoUA:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=9VZ-XZnkt6GCj2hCYH0A:9 a=wPNLvfGTeEIA:10 a=vWgntwq9fJasWC5-:21 a=xhvF60G8yZa7iZUg:21 a=0nF1XD0wxitMEM03M9B4ZQ==:117
X-AUTH mrabarnett:2500
Date Fri, 24 Feb 2012 18:36:10 +0000
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2
MIME-Version 1.0
To python-list@python.org
Subject Re: multiprocessing, what am I doing wrong?
References <CAAoZyYMHv=JCZ-PE4ufuuKmcR5Y3rZNNkDz24kv4uJdframKdA@mail.gmail.com> <4F46A49D.9030905@mrabarnett.plus.com> <CAAoZyYM43CjGuTKS9T65dPuSGjU-JrPNR9H2yC5K2z8RoVM=ew@mail.gmail.com>
In-Reply-To <CAAoZyYM43CjGuTKS9T65dPuSGjU-JrPNR9H2yC5K2z8RoVM=ew@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To python-list@python.org
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.135.1330108574.3037.python-list@python.org> (permalink)
Lines 70
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1330108574 news.xs4all.nl 6849 [2001:888:2000:d::a6]:37799
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:20828

Show key headers only | View raw


On 24/02/2012 17:00, Eric Frederich wrote:
> I can sill get it to freeze and nothing is printed out from the other
> except block.
> Does it look like I'm doing anything wrong here?
>
[snip]
I don't normally use multiprocessing, so I forgot about a critical
detail. :-(

When the multiprocessing module starts a process, that process
_imports_ the module which contains the function which is to be run, so
what's happening is that when your script is run, it creates and starts
workers, the multiprocessing module makes a new process for each
worker, each of those processes then imports the script, which creates
and starts workers, etc, leading to an ever-increasing number of
processes.

The solution is to ensure that the script/module distinguishes between
being run as the main script and being imported as a module:

#!/usr/bin/env python

import sys
import Queue
import multiprocessing
import time

def FOO(a, b, c):
     print 'foo', a, b, c
     return (a + b) * c

class MyWorker(multiprocessing.Process):
     def __init__(self, inbox, outbox):
         super(MyWorker, self).__init__()
         self.inbox = inbox
         self.outbox = outbox
         print >> sys.stderr, '1' * 80; sys.stderr.flush()
     def run(self):
         print >> sys.stderr, '2' * 80; sys.stderr.flush()
         while True:
             try:
                 args = self.inbox.get_nowait()
             except Queue.Empty:
                 break
             self.outbox.put(FOO(*args))

if __name__ == '__main__':
     # This file is being run as the main script. This part won't be
     # run if the file is imported.
     todo = multiprocessing.Queue()

     for i in xrange(100):
         todo.put((i, i+1, i+2))

     print >> sys.stderr, 'a' * 80; sys.stderr.flush()
     result_queue = multiprocessing.Queue()

     print >> sys.stderr, 'b' * 80; sys.stderr.flush()
     w1 = MyWorker(todo, result_queue)
     print >> sys.stderr, 'c' * 80; sys.stderr.flush()
     w2 = MyWorker(todo, result_queue)

     print >> sys.stderr, 'd' * 80; sys.stderr.flush()
     w1.start()
     print >> sys.stderr, 'e' * 80; sys.stderr.flush()
     w2.start()
     print >> sys.stderr, 'f' * 80; sys.stderr.flush()

     for i in xrange(100):
         print result_queue.get()

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


Thread

Re: multiprocessing, what am I doing wrong? MRAB <python@mrabarnett.plus.com> - 2012-02-24 18:36 +0000

csiph-web