diff --git a/Doc/library/multiprocessing.rst b/Doc/library/multiprocessing.rst index 66c73a1..4920e95 100644 --- a/Doc/library/multiprocessing.rst +++ b/Doc/library/multiprocessing.rst @@ -297,7 +297,7 @@ The :mod:`multiprocessing` package mostly replicates the API of the :class:`Process` and exceptions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. class:: Process([group[, target[, name[, args[, kwargs]]]]]) +.. class:: Process([group[, target[, name[, args[, kwargs[, daemon]]]]]]) Process objects represent activity that is run in a separate process. The :class:`Process` class has equivalents of all the methods of @@ -312,8 +312,11 @@ The :mod:`multiprocessing` package mostly replicates the API of the :sub:`1`,N\ :sub:`2`,...,N\ :sub:`k` is a sequence of integers whose length is determined by the *generation* of the process. *args* is the argument tuple for the target invocation. *kwargs* is a dictionary of keyword - arguments for the target invocation. By default, no arguments are passed to - *target*. + arguments for the target invocation. *daemon* sets the process + :attr:`daemon` flag, if not provided this will be inherited from the + creating process. + + By default, no arguments are passed to *target*. If a subclass overrides the constructor, it must make sure it invokes the base class constructor (:meth:`Process.__init__`) before doing anything else diff --git a/Lib/multiprocessing/process.py b/Lib/multiprocessing/process.py index 0b04e36..dcd95dd 100644 --- a/Lib/multiprocessing/process.py +++ b/Lib/multiprocessing/process.py @@ -59,18 +59,22 @@ def _cleanup(): class Process(object): ''' - Process objects represent activity that is run in a separate process + Process objects represent activity that is run in a separate process. - The class is analagous to `threading.Thread` + This class is analogous to ``threading.Thread``. ''' _Popen = None - def __init__(self, group=None, target=None, name=None, args=(), kwargs={}): + def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, + daemon=False): assert group is None, 'group argument must be None for now' count = _current_process._counter.next() self._identity = _current_process._identity + (count,) self._authkey = _current_process._authkey - self._daemonic = _current_process._daemonic + if daemon is not None: + self._daemonic = daemon + else: + self._daemonic = _current_process._daemonic self._tempdir = _current_process._tempdir self._parent_pid = os.getpid() self._popen = None diff --git a/Lib/test/test_multiprocessing.py b/Lib/test/test_multiprocessing.py index b16629b..223ccbf 100644 --- a/Lib/test/test_multiprocessing.py +++ b/Lib/test/test_multiprocessing.py @@ -137,6 +137,19 @@ class _TestProcess(BaseTestCase): self.assertEqual(current.ident, os.getpid()) self.assertEqual(current.exitcode, None) + def test_daemon_keyword_argument(self): + if self.TYPE == "threads": + return + + proc1 = self.Process(target=self._test, daemon=True) + self.assertTrue(proc1.daemon) + proc2 = self.Process(target=self._test, daemon=False) + self.assertFalse(proc2.daemon) + + # By default uses the current process's daemon flag. + proc3 = self.Process(target=self._test) + self.assertEquals(proc3.daemon, self.current_process().daemon) + def _test(self, q, *args, **kwds): current = self.current_process() q.put(args)