Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'python3': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'ugly': 0.07; 'val': 0.07; '-1,': 0.09; '[1,': 0.09; 'file"': 0.09; 'identifier': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.14; 'do,': 0.15; 'file,': 0.15; '"error': 0.16; '(write': 0.16; 'cc:name:python list': 0.16; 'dirs,': 0.16; 'file1': 0.16; 'folks,': 0.16; 'forth.': 0.16; 'threw': 0.16; 'wrote:': 0.16; 'directory.': 0.18; ';-)': 0.18; 'math': 0.20; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; 'stopping': 0.22; '2015': 0.23; 'errors': 0.23; 'file.': 0.24; 'import': 0.24; 'seems': 0.24; 'header:In-Reply-To:1': 0.24; 'raise': 0.24; 'developing': 0.25; 'error': 0.27; 'skip:# 10': 0.27; 'switch': 0.27; 'message-id:@mail.gmail.com': 0.28; 'etc).': 0.29; 'follows': 0.29; 'other,': 0.29; 'ret': 0.29; 'handled': 0.29; 'minimal': 0.31; 'task': 0.31; 'writes': 0.31; "i'd": 0.31; 'error.': 0.31; 'operations': 0.31; 'run': 0.32; "can't": 0.32; 'skip:[ 10': 0.32; 'info': 0.33; 'problem': 0.33; 'raising': 0.33; 'wrap': 0.33; 'open': 0.33; '(for': 0.34; 'file': 0.34; 'received:google.com': 0.34; 'could': 0.35; 'generic': 0.35; 'replace': 0.35; 'tasks': 0.35; 'something': 0.35; 'expected': 0.35; 'sometimes': 0.35; 'but': 0.36; 'possible.': 0.36; 'tool': 0.36; 'there': 0.36; 'statement': 0.36; 'quite': 0.37; 'should': 0.37; 'subject:: ': 0.37; 'thought': 0.37; 'rather': 0.38; 'stuff': 0.38; 'someone': 0.38; 'shared': 0.38; 'whatever': 0.39; 'subject:-': 0.39; 'subject:with': 0.40; 'skip:t 20': 0.40; 'some': 0.40; 'even': 0.61; 'behavior': 0.61; 'simple': 0.61; 'more': 0.62; 'to,': 0.63; 'between': 0.65; 'tasks.': 0.66; 'special': 0.72; 'interface:': 0.84; 'oscar': 0.84; 'hundred': 0.96 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=daXkm8otU0ekA0tTIuMEKWjyTJhX9KcHwwrt2y9jJN4=; b=R3LXBBEX0og+PDC3shSqAYU6M8GAJ1ZYGMRXcSr++Ffqp5qIDw1HHNb4KoczSjgWw1 1CdvvzvApjOscKu3FVUQG9h9jE/84v3nGSCnbsWx03mlMl87kkaaT7Hc9CSABLbWuGgc I366eWhbmVj9Rf2XRzieyrKpPzvMV19jK5SUJOuB/jPzxp1dtR+dMOcAD7pxy1Y0zaEf iqUNYv08Hthj0OqKa1eHsMaXgh6GVhnHZLBkL1fECIoOgdIWbRcE1XafyXi0z3+fLf4q JSXDOu7EpWeydcOkGPzFV5OCEAd6FjX8hPJJ+t7BvzMKiV/b+PS1JQhtbqolJ2nxCspn 3VoQ== X-Received: by 10.112.118.206 with SMTP id ko14mr18332999lbb.28.1434726097942; Fri, 19 Jun 2015 08:01:37 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Oscar Benjamin Date: Fri, 19 Jun 2015 16:01:17 +0100 Subject: Re: Catching exceptions with multi-processing To: Fabien Cc: Python List Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 70 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1434726105 news.xs4all.nl 2915 [2001:888:2000:d::a6]:45154 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!news.stben.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:92887 On 19 June 2015 at 15:01, Fabien wrote: > Folks, > > I am developing a tool which works on individual entities (glaciers) and do > a lot of operations on them. There are many tasks to do, one after each > other, and each task follows the same interface: > > def task_1(path_to_glacier_dir): > open file1 in path_to_glacier_dir > do stuff > if dont_work: > raise RuntimeError("didnt work") > write file2 in path_to_glacier_dir > > This way, the tasks can be run in parallel very easily: > > import multiprocessing as mp > pool = mp.Pool(4) > > dirs = [list_of_dirs] > pool.map(task1, dirs, chunksize=1) > pool.map(task2, dirs, chunksize=1) > pool.map(task3, dirs, chunksize=1) > > ... and so forth. I tested the tool for about a hundred glaciers but now it > has to run for thousands of them. There are going to be errors, some of them > are even expected for special outliers. What I would like the tool to do is > that in case of error, it writes the identifier of the problematic glacier > somewhere, the error encountered and more info if possible. Because of > multiprocessing, I can't write in a shared file, so I thought that the > individual processes should write a unique "error file" in a dedicated > directory. > > What I don't know how to, however, is how to do this at minimal cost and in > a generic way for all tasks. Also, the task2 should not be run if task1 > threw an error. Sometimes (for debugging), I'd rather keep the normal > behavior of raising an error and stopping the program. > > Do I have to wrap all tasks with a "try: exept:" block? How to switch > between behaviors? All the solutions I could think about look quite ugly to > me. And it seems that this is a general problem that someone cleverer than > me had solved before ;-) A simple way to approach this could be something like: #!/usr/bin/env python3 import math import multiprocessing def sqrt(x): if x < 0: return 'error', x else: return 'success', math.sqrt(x) if __name__ == "__main__": numbers = [1, 2, 3, -1, -3] pool = multiprocessing.Pool() for ret, val in pool.imap(sqrt, numbers): if ret == 'error': raise ValueError(val) print(val) Just replace the raise statement with whatever you want to do (write to a file etc). Since all errors are handled in the master process there are no issues with writing to a file. -- Oscar