Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:using': 0.04; 'attribute': 0.07; 'admit': 0.09; 'problem:': 0.09; 'exception': 0.12; 'skip:f 30': 0.13; 'def': 0.15; 'skip:" 40': 0.15; 'class:': 0.16; 'deadlock': 0.16; 'deadlock.': 0.16; 'dumps': 0.16; 'lookup': 0.16; 'run(self):': 0.16; 'cc:addr:python-list': 0.16; 'subject:problem': 0.19; 'defined': 0.19; 'received:74.125.82.174': 0.19; 'received:mail- wy0-f174.google.com': 0.19; 'cc:no real name:2**0': 0.20; '(most': 0.21; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'module,': 0.23; 'traceback': 0.24; 'sender:addr:gmail.com': 0.25; 'function': 0.27; 'skip:_ 20': 0.28; 'skip:" 30': 0.28; 'correct': 0.28; 'import': 0.28; 'message-id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.30; 'seemingly': 0.30; 'class': 0.30; 'yet': 0.32; 'error.': 0.32; "can't": 0.33; 'there': 0.33; 'creates': 0.34; 'test': 0.34; 'last):': 0.34; 'file': 0.36; 'sequence': 0.37; 'subject:skip:m 10': 0.37; 'thread': 0.37; 'using': 0.37; 'run': 0.37; 'received:74.125.82': 0.38; 'forgive': 0.38; 'programming,': 0.38; 'received:google.com': 0.38; 'subject:: ': 0.39; 'manually': 0.39; "there's": 0.39; 'why': 0.39; 'received:74.125': 0.39; 'where': 0.40; 'touch': 0.68; 'skip:\xc2 10': 0.74; 'questions:': 0.84; 'besides,': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type :content-transfer-encoding; bh=87FOgGbL+ijvCyOpJBNUQKRU+furNQ/+sQ30cN2IcA4=; b=jqTh0u36UnhGNh0+sif1XPxXKfksfYSywAdXTiSZpdM/qwC5O76h1Wj6sdwAxtOqZ3 mfGsYV1yMIr9tOUTBn4SLwr+SSbdbAMHdd5Bea84xudCmPkEeS5In7vvAYoRj8sFRLOv JLbB1DrZaQET4+OlsWrlPWJOcUncR+kPbQl+k= MIME-Version: 1.0 Sender: kushal.kumaran@gmail.com In-Reply-To: <5ccb9d98-f3f8-4f86-b53c-cda4522b40fc@14g2000prv.googlegroups.com> References: <5ccb9d98-f3f8-4f86-b53c-cda4522b40fc@14g2000prv.googlegroups.com> From: Kushal Kumaran Date: Sun, 11 Sep 2011 17:02:18 +0530 X-Google-Sender-Auth: kuOqOE3pHUgD5V4k7GgUa4WzXbw Subject: Re: Deadlock problem using multiprocessing To: =?UTF-8?B?6JOd6Imy5Z+65Zug?= Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 82 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1315740760 news.xs4all.nl 2538 [2001:888:2000:d::a6]:54486 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:13121 2011/9/11 =E8=93=9D=E8=89=B2=E5=9F=BA=E5=9B=A0 : > This is my first touch on the multiprocessing module, and I admit not > having a deep understanding of parallel programming, forgive me if > there's any obvious error. This is my test code: > > # deadlock.py > > import multiprocessing > > class MPTask: > =C2=A0 =C2=A0 =C2=A0 =C2=A0def __init__(self): > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self._tseq=3D rang= e(10) =C2=A0 # task sequence > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self._pool=3D mult= iprocessing.Pool(2) =C2=A0 =C2=A0 # process pool > =C2=A0 =C2=A0 =C2=A0 =C2=A0def _exe(self, num): > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return num**2 > =C2=A0 =C2=A0 =C2=A0 =C2=A0def run(self): > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0result=3D self._po= ol.map_async(self._exe, self._tseq) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return result.get(= ) > > result=3D MPTask().run() > print(result) > > And seemingly it creates a deadlock, I have to manually kill the > processes in the system monitor, yet it would be OK if the _exe() > function was defined outside the MPTask class: > > # no_deadlock.py > import multiprocessing > > def _exe(num): > =C2=A0 =C2=A0 =C2=A0 =C2=A0return num**2 > > class MPTask: > =C2=A0 =C2=A0 =C2=A0 =C2=A0def __init__(self): > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self._tseq=3D rang= e(10) =C2=A0 # task sequence > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self._pool=3D mult= iprocessing.Pool(2) =C2=A0 =C2=A0 # process pool > =C2=A0 =C2=A0 =C2=A0 =C2=A0def run(self): > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0result=3D self._po= ol.map_async(_exe, self._tseq) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return result.get(= ) > > result=3D MPTask().run() > print(result) > > This would give the correct answer without any deadlock. My questions: > > 1. Why is this, where did the deadlock come from? > > 2. Besides, is there any material I can refer to about how to avoid > deadlocks when using multiple processes in the program? > I get this exception when I run the first program: Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib/python3.1/threading.py", line 516, in _bootstrap_inner self.run() File "/usr/lib/python3.1/threading.py", line 469, in run self._target(*self._args, **self._kwargs) File "/usr/lib/python3.1/multiprocessing/pool.py", line 231, in _handle_t= asks put(task) File "/usr/lib/python3.1/pickle.py", line 1349, in dumps Pickler(f, protocol, fix_imports=3Dfix_imports).dump(obj) _pickle.PicklingError: Can't pickle : attribute lookup builtins.method failed There is no deadlock. You are hitting this problem: http://stackoverflow.com/questions/1816958/cant-pickle-type-instancemethod-= when-using-pythons-multiprocessing-pool-map --=20 regards, kushal