Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'mrab': 0.05; '__name__': 0.09; 'friday,': 0.09; 'snippet': 0.09; 'subject:process': 0.09; 'whichever': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '#this': 0.16; "'__main__':": 0.16; 'check.': 0.16; 'finds': 0.16; 'for,': 0.16; 'inputs': 0.16; 'received:mac.com': 0.16; 'simplest': 0.16; 'subject:skip:m 10': 0.16; 'sys.exit(0)': 0.16; 'tried:': 0.16; 'try?': 0.16; 'typo': 0.16; 'wrote:': 0.18; 'received:10.0.1': 0.19; 'solution.': 0.20; 'written': 0.21; '>>>': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'cc:2**1': 0.23; 'creating': 0.23; 'looks': 0.24; 'cc:no real name:2**0': 0.24; 'defined': 0.27; 'correct': 0.29; 'am,': 0.29; 'url:mailman': 0.30; 'code': 0.31; 'os,': 0.31; 'prints': 0.31; 'file': 0.32; 'url:python': 0.33; 'maybe': 0.34; 'basic': 0.35; "can't": 0.35; 'problem.': 0.35; 'something': 0.35; 'there': 0.35; 'accessible': 0.36; 'url:listinfo': 0.36; 'charset:us- ascii': 0.36; 'received:10.0': 0.36; 'url:org': 0.36; 'detail': 0.37; 'so,': 0.37; 'received:10': 0.37; 'thank': 0.38; 'nov': 0.38; 'received:17': 0.38; 'anything': 0.39; 'bill': 0.39; 'sure': 0.39; 'url:mail': 0.40; 'solve': 0.60; 'simple': 0.61; 'further': 0.61; 'provide': 0.64; 'more': 0.64; 'different': 0.65; 'situation': 0.65; 'to:addr:gmail.com': 0.65; 'hang': 0.67; 'answer.': 0.68; 'periodically': 0.68; 'evaluate': 0.72; 'header :In-reply-to:1': 0.84; 'wakes': 0.84; '2013,': 0.91; 'subject:answer': 0.95; '2013': 0.98 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.10.8794,1.0.431,0.0.0000 definitions=2013-11-02_01:2013-11-02,2013-11-02,1970-01-01 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 suspectscore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=7.0.1-1308280000 definitions=main-1311020072 Content-type: text/plain; charset=us-ascii MIME-version: 1.0 (Mac OS X Mail 6.6 \(1510\)) Subject: Re: multiprocessing: child process race to answer From: William Ray Wing In-reply-to: Date: Sat, 02 Nov 2013 08:17:08 -0400 Content-transfer-encoding: quoted-printable References: To: smhall05 X-Mailer: Apple Mail (2.1510) Cc: python-list@python.org, William Ray Wing X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 59 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1383394638 news.xs4all.nl 15871 [2001:888:2000:d::a6]:58557 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:58324 On Nov 2, 2013, at 1:03 AM, smhall05 wrote: > On Friday, November 1, 2013 10:52:40 PM UTC-4, MRAB wrote: >> On 02/11/2013 02:35, smhall05 wrote: >>=20 >>> I am using a basic multiprocessing snippet I found: >>>=20 >>> #----------------------------------------------------- >>> from multiprocessing import Pool >>>=20 >>> def f(x): >>> return x*x >>>=20 >>> if __name__ =3D=3D '__main__': >>> pool =3D Pool(processes=3D4) # start 4 worker = processes >>> result =3D pool.apply_async(f, [10]) # evaluate "f(10)" = asynchronously >>> print result.get(timeout=3D1) >>> print pool.map(f, range(10)) # prints "[0, 1, 4,..., = 81]" >>> #--------------------------------------------------------- >>>=20 >>> I am using this code to have each process go off and solve the same = problem, just with different inputs to the problem. I need to be able to = kill all processes once 1 of n processes has come up with the solution. = There will only be one answer. >>>=20 >>> I have tried: >>>=20 >>> sys.exit(0) #this causes the program to hang >>> pool.close() >>> pool.terminate >>>=20 >>=20 >> Did you actually mean "pool.terminate", or is that a typo for >>=20 >> "pool.terminate()"? >>=20 >>> These still allow further processing before the program terminates. = What else can I try? I am not able to share the exact code at this time. = I can provide more detail if I am unclear. Thank you >>>=20 >=20 > I am not sure to be honest, however it turns out that I can't use = pool.terminate() because pool is defined in main and not accessible = under my def in which I check for the correct answer. > --=20 > https://mail.python.org/mailman/listinfo/python-list So, the simplest solution to that situation is to have whichever = subprocess that finds the correct answer set a flag which the calling = process can check. Depending on your OS, that flag can be anything from = setting a lock to something as simple as creating a file which the = calling process periodically wakes up and looks for, maybe just a file = in which the subprocess has written the answer. Bill