Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed7.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'url:pypi': 0.03; '[0]': 0.07; 'ugly': 0.07; 'file"': 0.09; 'identifier': 0.09; 'cc:addr :python-list': 0.10; 'exception': 0.13; 'def': 0.14; 'do,': 0.15; 'file,': 0.15; '"error': 0.16; 'consume': 0.16; 'dirs,': 0.16; 'file1': 0.16; 'folks,': 0.16; 'forth.': 0.16; 'handling.': 0.16; 'none).': 0.16; 'task:': 0.16; 'threw': 0.16; 'wrote:': 0.16; 'directory.': 0.18; ';-)': 0.18; 'thanks,': 0.19; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; 'arguments': 0.22; 'exceptions': 0.22; 'raised': 0.22; 'stopping': 0.22; 'pass': 0.22; 'am,': 0.23; '2015': 0.23; 'import': 0.24; 'seems': 0.24; 'header:In-Reply- To:1': 0.24; 'raise': 0.24; 'developing': 0.25; 'error': 0.27; 'switch': 0.27; 'message-id:@mail.gmail.com': 0.28; 'follows': 0.29; 'other,': 0.29; 'queue': 0.29; 'fri,': 0.31; 'minimal': 0.31; 'task': 0.31; 'writes': 0.31; 'url:mailman': 0.31; "i'd": 0.31; 'error.': 0.31; 'operations': 0.31; 'run': 0.32; "can't": 0.32; 'skip:[ 10': 0.32; 'twitter:': 0.32; 'framework': 0.32; 'info': 0.33; 'url:python': 0.33; 'problem': 0.33; 'raising': 0.33; 'wrap': 0.33; 'open': 0.33; '(for': 0.34; 'received:google.com': 0.34; 'url:listinfo': 0.35; 'could': 0.35; 'generic': 0.35; 'tasks': 0.35; 'expected': 0.35; 'sometimes': 0.35; 'but': 0.36; 'url:org': 0.36; 'project': 0.36; 'possible.': 0.36; 'tool': 0.36; 'there': 0.36; 'quite': 0.37; 'should': 0.37; 'subject:: ': 0.37; 'thought': 0.37; 'leader': 0.37; 'rather': 0.38; 'stuff': 0.38; 'someone': 0.38; 'shared': 0.38; 'application': 0.39; 'subject:-': 0.39; 'where': 0.40; 'subject:with': 0.40; 'skip:t 20': 0.40; 'some': 0.40; 'your': 0.60; 'even': 0.61; 'behavior': 0.61; 'more': 0.62; 'to,': 0.63; 'between': 0.65; 'tasks.': 0.66; 'special': 0.72; 'interface:': 0.84; 'succeeds': 0.84; 'audit': 0.93; '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:content-transfer-encoding; bh=ps9E+TWlWoDzd4w4Lww1IIp5Nb6e42i94LTQjpysJb0=; b=WgaI3jjvKwtr+RGu0gx4A274/2A/L88G96UDjicPGNXjxVCGy7zq4V8C0asAd5oiMg ZiAYy0OzhOwGensDvotzKhfychIlJixtr1hOsg+Tp5q0oVMLwK4p2TrlQjlHmy0tYEp/ bK1qorILkjKn30n9PWSQNxiu3dQGc5l5eczNgRkC9tsSHPmBjh+eKCq50Atitsf8VhuV CSdVOatLdMQ1ng/DVqVlaJFq7ARGh2bjqut0qfJ0eyFj/YRc818c1ggqFZFFLCwU4qxS zMoQrpTH6MNDwt2BbVH+XtWV3bGle+9/FzvBOAa2g5Twi6QNVi3JmoV91VDbfBF65Pqo 0GSg== X-Received: by 10.52.32.34 with SMTP id f2mr12877033vdi.11.1434723949895; Fri, 19 Jun 2015 07:25:49 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Andres Riancho Date: Fri, 19 Jun 2015 11:25:29 -0300 Subject: Re: Catching exceptions with multi-processing To: Fabien Cc: Python Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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: 90 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1434723958 news.xs4all.nl 2881 [2001:888:2000:d::a6]:33707 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92885 Fabien, My recommendation is that you should pass some extra arguments to the ta= sk: * A unique task id * A result multiprocessing.Queue When an exception is raised you put (unique_id, exception) to the queue. When it succeeds you put (unique_id, None). In the main process you consume the queue and do your error handling. Note that some exceptions can't be serialized, there is where tblib [0] comes handy. [0] https://pypi.python.org/pypi/tblib Regards, On Fri, Jun 19, 2015 at 11:01 AM, 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 =3D mp.Pool(4) > > dirs =3D [list_of_dirs] > pool.map(task1, dirs, chunksize=3D1) > pool.map(task2, dirs, chunksize=3D1) > pool.map(task3, dirs, chunksize=3D1) > > ... 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 t= hem > 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 glacie= r > 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 tha= n > me had solved before ;-) > > Thanks, > > Fabien > > > > > > > > -- > https://mail.python.org/mailman/listinfo/python-list --=20 Andr=C3=A9s Riancho Project Leader at w3af - http://w3af.org/ Web Application Attack and Audit Framework Twitter: @w3af GPG: 0x93C344F3