Mercurial > cpython-lru_cache-weakref
annotate Lib/multiprocessing/process.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | da5b370f41a1 |
| children | 0fa82a06194c |
| rev | line source |
|---|---|
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
1 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
2 # Module providing the `Process` class which emulates `threading.Thread` |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
3 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
4 # multiprocessing/process.py |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
5 # |
|
66887
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
6 # Copyright (c) 2006-2008, R Oudkerk |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
7 # All rights reserved. |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
8 # |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
9 # Redistribution and use in source and binary forms, with or without |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
10 # modification, are permitted provided that the following conditions |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
11 # are met: |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
12 # |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
13 # 1. Redistributions of source code must retain the above copyright |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
14 # notice, this list of conditions and the following disclaimer. |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
15 # 2. Redistributions in binary form must reproduce the above copyright |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
16 # notice, this list of conditions and the following disclaimer in the |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
17 # documentation and/or other materials provided with the distribution. |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
18 # 3. Neither the name of author nor the names of any contributors may be |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
19 # used to endorse or promote products derived from this software |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
20 # without specific prior written permission. |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
21 # |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
22 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
23 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
24 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
25 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
26 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
27 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
28 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
29 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
31 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
59330
diff
changeset
|
32 # SUCH DAMAGE. |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
33 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
34 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
35 __all__ = ['Process', 'current_process', 'active_children'] |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
36 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
37 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
38 # Imports |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
39 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
40 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
41 import os |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
42 import sys |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
43 import signal |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
44 import itertools |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
45 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
46 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
47 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
48 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
49 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
50 try: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
51 ORIGINAL_DIR = os.path.abspath(os.getcwd()) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
52 except OSError: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
53 ORIGINAL_DIR = None |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
54 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
55 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
56 # Public functions |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
57 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
58 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
59 def current_process(): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
60 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
61 Return process object representing the current process |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
62 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
63 return _current_process |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
64 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
65 def active_children(): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
66 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
67 Return list of process objects corresponding to live child processes |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
68 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
69 _cleanup() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
70 return list(_current_process._children) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
71 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
72 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
73 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
74 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
75 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
76 def _cleanup(): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
77 # check for processes which have finished |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
78 for p in list(_current_process._children): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
79 if p._popen.poll() is not None: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
80 _current_process._children.discard(p) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
81 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
82 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
83 # The `Process` class |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
84 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
85 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
86 class Process(object): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
87 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
88 Process objects represent activity that is run in a separate process |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
89 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
90 The class is analagous to `threading.Thread` |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
91 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
92 _Popen = None |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
93 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
94 def __init__(self, group=None, target=None, name=None, args=(), kwargs={}): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
95 assert group is None, 'group argument must be None for now' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
96 count = _current_process._counter.next() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
97 self._identity = _current_process._identity + (count,) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
98 self._authkey = _current_process._authkey |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
99 self._daemonic = _current_process._daemonic |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
100 self._tempdir = _current_process._tempdir |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
101 self._parent_pid = os.getpid() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
102 self._popen = None |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
103 self._target = target |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
104 self._args = tuple(args) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
105 self._kwargs = dict(kwargs) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
106 self._name = name or type(self).__name__ + '-' + \ |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
107 ':'.join(str(i) for i in self._identity) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
108 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
109 def run(self): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
110 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
111 Method to be run in sub-process; can be overridden in sub-class |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
112 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
113 if self._target: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
114 self._target(*self._args, **self._kwargs) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
115 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
116 def start(self): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
117 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
118 Start child process |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
119 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
120 assert self._popen is None, 'cannot start a process twice' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
121 assert self._parent_pid == os.getpid(), \ |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
122 'can only start a process object created by current process' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
123 assert not _current_process._daemonic, \ |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
124 'daemonic processes are not allowed to have children' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
125 _cleanup() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
126 if self._Popen is not None: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
127 Popen = self._Popen |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
128 else: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
129 from .forking import Popen |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
130 self._popen = Popen(self) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
131 _current_process._children.add(self) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
132 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
133 def terminate(self): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
134 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
135 Terminate process; sends SIGTERM signal or uses TerminateProcess() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
136 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
137 self._popen.terminate() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
138 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
139 def join(self, timeout=None): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
140 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
141 Wait until child process terminates |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
142 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
143 assert self._parent_pid == os.getpid(), 'can only join a child process' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
144 assert self._popen is not None, 'can only join a started process' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
145 res = self._popen.wait(timeout) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
146 if res is not None: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
147 _current_process._children.discard(self) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
148 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
149 def is_alive(self): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
150 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
151 Return whether process is alive |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
152 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
153 if self is _current_process: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
154 return True |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
155 assert self._parent_pid == os.getpid(), 'can only test a child process' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
156 if self._popen is None: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
157 return False |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
158 self._popen.poll() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
159 return self._popen.returncode is None |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
160 |
|
48302
7ea805d879e1
issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents:
47823
diff
changeset
|
161 @property |
|
7ea805d879e1
issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents:
47823
diff
changeset
|
162 def name(self): |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
163 return self._name |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
164 |
|
48302
7ea805d879e1
issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents:
47823
diff
changeset
|
165 @name.setter |
|
7ea805d879e1
issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents:
47823
diff
changeset
|
166 def name(self, name): |
|
57922
427c2143e2c9
Merged revisions 77038 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55385
diff
changeset
|
167 assert isinstance(name, basestring), 'name must be a string' |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
168 self._name = name |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
169 |
|
48302
7ea805d879e1
issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents:
47823
diff
changeset
|
170 @property |
|
7ea805d879e1
issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents:
47823
diff
changeset
|
171 def daemon(self): |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
172 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
173 Return whether process is a daemon |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
174 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
175 return self._daemonic |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
176 |
|
48302
7ea805d879e1
issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents:
47823
diff
changeset
|
177 @daemon.setter |
|
7ea805d879e1
issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents:
47823
diff
changeset
|
178 def daemon(self, daemonic): |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
179 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
180 Set whether process is a daemon |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
181 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
182 assert self._popen is None, 'process has already started' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
183 self._daemonic = daemonic |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
184 |
|
48302
7ea805d879e1
issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents:
47823
diff
changeset
|
185 @property |
|
7ea805d879e1
issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents:
47823
diff
changeset
|
186 def authkey(self): |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
187 return self._authkey |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
188 |
|
48302
7ea805d879e1
issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents:
47823
diff
changeset
|
189 @authkey.setter |
|
7ea805d879e1
issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents:
47823
diff
changeset
|
190 def authkey(self, authkey): |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
191 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
192 Set authorization key of process |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
193 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
194 self._authkey = AuthenticationString(authkey) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
195 |
|
48302
7ea805d879e1
issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents:
47823
diff
changeset
|
196 @property |
|
7ea805d879e1
issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents:
47823
diff
changeset
|
197 def exitcode(self): |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
198 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
199 Return exit code of process or `None` if it has yet to stop |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
200 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
201 if self._popen is None: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
202 return self._popen |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
203 return self._popen.poll() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
204 |
|
48302
7ea805d879e1
issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents:
47823
diff
changeset
|
205 @property |
|
7ea805d879e1
issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents:
47823
diff
changeset
|
206 def ident(self): |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
207 ''' |
|
59330
295d59fa6e62
#7805: wait until all workers are started before collecting their PIDs
Florent Xicluna <florent.xicluna@gmail.com>
parents:
57922
diff
changeset
|
208 Return identifier (PID) of process or `None` if it has yet to start |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
209 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
210 if self is _current_process: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
211 return os.getpid() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
212 else: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
213 return self._popen and self._popen.pid |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
214 |
|
48302
7ea805d879e1
issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents:
47823
diff
changeset
|
215 pid = ident |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
216 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
217 def __repr__(self): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
218 if self is _current_process: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
219 status = 'started' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
220 elif self._parent_pid != os.getpid(): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
221 status = 'unknown' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
222 elif self._popen is None: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
223 status = 'initial' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
224 else: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
225 if self._popen.poll() is not None: |
|
48302
7ea805d879e1
issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents:
47823
diff
changeset
|
226 status = self.exitcode |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
227 else: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
228 status = 'started' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
229 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
230 if type(status) is int: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
231 if status == 0: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
232 status = 'stopped' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
233 else: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
234 status = 'stopped[%s]' % _exitcode_to_name.get(status, status) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
235 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
236 return '<%s(%s, %s%s)>' % (type(self).__name__, self._name, |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
237 status, self._daemonic and ' daemon' or '') |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
238 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
239 ## |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
240 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
241 def _bootstrap(self): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
242 from . import util |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
243 global _current_process |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
244 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
245 try: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
246 self._children = set() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
247 self._counter = itertools.count(1) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
248 try: |
|
55385
fda9062ebfa4
Merged revisions 73708,73738 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
48302
diff
changeset
|
249 sys.stdin.close() |
|
fda9062ebfa4
Merged revisions 73708,73738 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
48302
diff
changeset
|
250 sys.stdin = open(os.devnull) |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
251 except (OSError, ValueError): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
252 pass |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
253 _current_process = self |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
254 util._finalizer_registry.clear() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
255 util._run_after_forkers() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
256 util.info('child process calling self.run()') |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
257 try: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
258 self.run() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
259 exitcode = 0 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
260 finally: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
261 util._exit_function() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
262 except SystemExit, e: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
263 if not e.args: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
264 exitcode = 1 |
|
77378
da5b370f41a1
Issue #13854: Properly handle non-integer, non-string arg to SystemExit
Richard Oudkerk <shibturn@gmail.com>
parents:
66887
diff
changeset
|
265 elif isinstance(e.args[0], int): |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
266 exitcode = e.args[0] |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
267 else: |
|
77378
da5b370f41a1
Issue #13854: Properly handle non-integer, non-string arg to SystemExit
Richard Oudkerk <shibturn@gmail.com>
parents:
66887
diff
changeset
|
268 sys.stderr.write(str(e.args[0]) + '\n') |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
269 sys.stderr.flush() |
|
77378
da5b370f41a1
Issue #13854: Properly handle non-integer, non-string arg to SystemExit
Richard Oudkerk <shibturn@gmail.com>
parents:
66887
diff
changeset
|
270 exitcode = 0 if isinstance(e.args[0], str) else 1 |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
271 except: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
272 exitcode = 1 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
273 import traceback |
|
48302
7ea805d879e1
issue3352: clean up the multiprocessing API to remove many get_/set_ methods and convert them to properties. Update the docs and the examples included.
Jesse Noller <jnoller@gmail.com>
parents:
47823
diff
changeset
|
274 sys.stderr.write('Process %s:\n' % self.name) |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
275 sys.stderr.flush() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
276 traceback.print_exc() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
277 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
278 util.info('process exiting with exitcode %d' % exitcode) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
279 return exitcode |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
280 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
281 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
282 # We subclass bytes to avoid accidental transmission of auth keys over network |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
283 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
284 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
285 class AuthenticationString(bytes): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
286 def __reduce__(self): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
287 from .forking import Popen |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
288 if not Popen.thread_is_spawning(): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
289 raise TypeError( |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
290 'Pickling an AuthenticationString object is ' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
291 'disallowed for security reasons' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
292 ) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
293 return AuthenticationString, (bytes(self),) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
294 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
295 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
296 # Create object representing the main process |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
297 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
298 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
299 class _MainProcess(Process): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
300 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
301 def __init__(self): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
302 self._identity = () |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
303 self._daemonic = False |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
304 self._name = 'MainProcess' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
305 self._parent_pid = None |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
306 self._popen = None |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
307 self._counter = itertools.count(1) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
308 self._children = set() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
309 self._authkey = AuthenticationString(os.urandom(32)) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
310 self._tempdir = None |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
311 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
312 _current_process = _MainProcess() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
313 del _MainProcess |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
314 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
315 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
316 # Give names to some return codes |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
317 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
318 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
319 _exitcode_to_name = {} |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
320 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
321 for name, signum in signal.__dict__.items(): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
322 if name[:3]=='SIG' and '_' not in name: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
323 _exitcode_to_name[-signum] = name |
