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


Groups > comp.lang.python > #28949

Re: forked processes and testing

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <kushal.kumaran@gmail.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; 'else:': 0.03; 'exception': 0.03; '"""': 0.05; 'decorator': 0.07; 'exit': 0.07; 'function,': 0.07; 'raises': 0.07; 'run,': 0.07; '**kwargs)': 0.09; '**kwargs):': 0.09; 'caller.': 0.09; 'fork': 0.09; 'sep': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'useful,': 0.13; 'run.': 0.16; 'sink': 0.16; 'sys.exit(0)': 0.16; 'why,': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'creates': 0.18; 'tests': 0.18; 'sender:addr:gmail.com': 0.18; 'import': 0.21; 'received:209.85.214.174': 0.21; 'runs': 0.22; 'skip:_ 20': 0.22; 'cc:2**0': 0.23; 'example': 0.23; 'seems': 0.23; 'idea': 0.24; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'wrote': 0.26; 'reaches': 0.27; 'message- id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'run': 0.28; 'email name:': 0.29; 'url:mailman': 0.29; 'case,': 0.29; 'integration': 0.29; 'skip:_ 10': 0.29; 'probably': 0.29; "i'm": 0.29; 'function': 0.30; 'code': 0.31; 'url:python': 0.32; 'running': 0.32; 'url:listinfo': 0.32; 'skip:s 30': 0.33; 'received:google.com': 0.34; 'process,': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'except': 0.36; 'but': 0.36; 'url:org': 0.36; '12,': 0.36; 'child': 0.36; 'test': 0.36; 'should': 0.36; 'quite': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'skip:o 20': 0.38; 'sure': 0.38; 'gives': 0.39; 'instead': 0.39; 'received:209.85.214': 0.39; 'takes': 0.39; 'header:Received:5': 0.40; 'url:mail': 0.40; 'your': 0.60; 'son': 0.60; 'back': 0.62; 'production': 0.63; 'andrea': 0.84; 'harness': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type; bh=e3Lwxh6r9wIeVA/x97cYTGRYBPvg2dn8qA+Ll2XmSl0=; b=LCrzeJKB11ZB5nhk5zPsa8hx7tWJaqp5pfRVbFfnJ8LP1QUaemV7T1LM3UfuJlWlUL g57FoOHBSykvfkkeXiCLTIjWQxP1MSSv7jYqu7WcCeQE4LSeqwoxuvK8KnA4ZSO5+a/Z WuyC5got4YDLYI8ZWlyi58+T29iCQK31AmvjjEFd8XL2cVTNHGmXfe5tq68wOZ7oWuhb GuRRzcPQxZRjE5OuG/wDSFUBuyjwgmxJXLhIvdABuYJKRclc5XQJdUyzdcEnRkFPsFYA 3IrLqRQAeXrbbviUoP6QfXMTTUMHL+cqvPbLgn6v45Lq30D9uuOxnxf3WDDnIep3CCEH /lDw==
MIME-Version 1.0
Sender kushal.kumaran@gmail.com
In-Reply-To <CAF_E5JbvAXKLThkcUk2OcfJQPAt=DYUZrBZ3RCLFqCsA+F1t4w@mail.gmail.com>
References <CAF_E5JbvAXKLThkcUk2OcfJQPAt=DYUZrBZ3RCLFqCsA+F1t4w@mail.gmail.com>
From Kushal Kumaran <kushal.kumaran+python@gmail.com>
Date Wed, 12 Sep 2012 17:01:05 +0530
X-Google-Sender-Auth 6ehRTKP0zQ58mf1j04GRLH-_Asc
Subject Re: forked processes and testing
To andrea crotti <andrea.crotti.0@gmail.com>
Content-Type text/plain; charset=UTF-8
Cc python-list <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
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.552.1347449491.27098.python-list@python.org> (permalink)
Lines 63
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1347449491 news.xs4all.nl 6940 [2001:888:2000:d::a6]:50397
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:28949

Show key headers only | View raw


On Wed, Sep 12, 2012 at 3:50 PM, andrea crotti
<andrea.crotti.0@gmail.com> wrote:
> I wrote a decorator that takes a function, run it in a forked process
> and return the PID:
>
> def on_forked_process(func):
>     from os import fork
>     """Decorator that forks the process, runs the function and gives
>     back control to the main process
>     """
>     def _on_forked_process(*args, **kwargs):
>         pid = fork()
>         if pid == 0:
>             func(*args, **kwargs)
>             sys.exit(0)
>         else:
>             return pid
>
>     return _on_forked_process
>
> It seems in general to work but I'm not able to test it, for example this fails:
>
> def test_on_forked_process(self):
>     @utils.on_forked_process
>     def _dummy_func():
>         pass
>
>     with self.assertRaises(SystemExit):
>         retpid = _dummy_func()
>         # pid of the son process should be always > 0
>         self.assertTrue(retpid > 0)
>
>
> and I'm not sure why, nose doesn't like the Exit apparently even if
> it's happening in an unrelated proces..
>
> Any idea of how to make it testable or improve it?
>

Use os._exit instead of sys.exit in your child process.  Remember that
fork creates a new process that is running the same code as the
original process.  In your case, the child process also has your test
harness as the caller.  sys.exit raises an exception that reaches the
test harness in the child process, which interferes with your test
run.

> In theory probably I will not use it for production because I should
> use something smarter to control the various processes I need to run,
> but for my integration tests it's quite useful, because then I can
> kill the processes like
>
>     except KeyboardInterrupt:
>         from os import kill
>         from signal import SIGTERM
>         print("Killing sink and worker")
>         kill(sink_pid, SIGTERM)
>         kill(worker_pid, SIGTERM)
> --
> http://mail.python.org/mailman/listinfo/python-list

-- 
regards,
kushal

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


Thread

Re: forked processes and testing Kushal Kumaran <kushal.kumaran+python@gmail.com> - 2012-09-12 17:01 +0530

csiph-web