Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #28949 > unrolled thread
| Started by | Kushal Kumaran <kushal.kumaran+python@gmail.com> |
|---|---|
| First post | 2012-09-12 17:01 +0530 |
| Last post | 2012-09-12 17:01 +0530 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: forked processes and testing Kushal Kumaran <kushal.kumaran+python@gmail.com> - 2012-09-12 17:01 +0530
| From | Kushal Kumaran <kushal.kumaran+python@gmail.com> |
|---|---|
| Date | 2012-09-12 17:01 +0530 |
| Subject | Re: forked processes and testing |
| Message-ID | <mailman.552.1347449491.27098.python-list@python.org> |
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 top | Article view | comp.lang.python
csiph-web