Mercurial > cpython-lru_cache-weakref
annotate Lib/multiprocessing/util.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 | 9ac8699d88d6 |
| children | 0a58fa8e9bac |
| 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 various facilities to other parts of the package |
|
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/util.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:
52526
diff
changeset
|
6 # Copyright (c) 2006-2008, R Oudkerk |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
52526
diff
changeset
|
7 # All rights reserved. |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
52526
diff
changeset
|
8 # |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
52526
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:
52526
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:
52526
diff
changeset
|
11 # are met: |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
52526
diff
changeset
|
12 # |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
52526
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:
52526
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:
52526
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:
52526
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:
52526
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:
52526
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:
52526
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:
52526
diff
changeset
|
20 # without specific prior written permission. |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
52526
diff
changeset
|
21 # |
|
9ac8699d88d6
Merged revisions 87225 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
52526
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:
52526
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:
52526
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:
52526
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:
52526
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:
52526
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:
52526
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:
52526
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:
52526
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:
52526
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:
52526
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 import itertools |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
36 import weakref |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
37 import atexit |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
38 import threading # we want threading to install it's |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
39 # cleanup function before multiprocessing does |
|
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 from multiprocessing.process import current_process, active_children |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
42 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
43 __all__ = [ |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
44 'sub_debug', 'debug', 'info', 'sub_warning', 'get_logger', |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
45 'log_to_stderr', 'get_temp_dir', 'register_after_fork', |
|
52526
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
46 'is_exiting', 'Finalize', 'ForkAwareThreadLock', 'ForkAwareLocal', |
|
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
47 'SUBDEBUG', 'SUBWARNING', |
|
47696
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 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
51 # Logging |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
52 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
53 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
54 NOTSET = 0 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
55 SUBDEBUG = 5 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
56 DEBUG = 10 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
57 INFO = 20 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
58 SUBWARNING = 25 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
59 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
60 LOGGER_NAME = 'multiprocessing' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
61 DEFAULT_LOGGING_FORMAT = '[%(levelname)s/%(processName)s] %(message)s' |
|
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 _logger = None |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
64 _log_to_stderr = False |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
65 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
66 def sub_debug(msg, *args): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
67 if _logger: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
68 _logger.log(SUBDEBUG, msg, *args) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
69 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
70 def debug(msg, *args): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
71 if _logger: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
72 _logger.log(DEBUG, msg, *args) |
|
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 def info(msg, *args): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
75 if _logger: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
76 _logger.log(INFO, msg, *args) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
77 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
78 def sub_warning(msg, *args): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
79 if _logger: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
80 _logger.log(SUBWARNING, msg, *args) |
|
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 def get_logger(): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
83 ''' |
|
48931
51f795c9530e
revert r66114 for Jesse
Benjamin Peterson <benjamin@python.org>
parents:
48930
diff
changeset
|
84 Returns logger used by multiprocessing |
|
47696
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 global _logger |
|
52526
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
87 import logging, atexit |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
88 |
|
52526
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
89 logging._acquireLock() |
|
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
90 try: |
|
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
91 if not _logger: |
|
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
92 |
|
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
93 _logger = logging.getLogger(LOGGER_NAME) |
|
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
94 _logger.propagate = 0 |
|
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
95 logging.addLevelName(SUBDEBUG, 'SUBDEBUG') |
|
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
96 logging.addLevelName(SUBWARNING, 'SUBWARNING') |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
97 |
|
52526
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
98 # XXX multiprocessing should cleanup before logging |
|
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
99 if hasattr(atexit, 'unregister'): |
|
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
100 atexit.unregister(_exit_function) |
|
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
101 atexit.register(_exit_function) |
|
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
102 else: |
|
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
103 atexit._exithandlers.remove((_exit_function, (), {})) |
|
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
104 atexit._exithandlers.append((_exit_function, (), {})) |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
105 |
|
52526
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
106 finally: |
|
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
107 logging._releaseLock() |
|
47696
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 return _logger |
|
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 def log_to_stderr(level=None): |
|
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 Turn on logging and add a handler which prints to stderr |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
114 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
115 global _log_to_stderr |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
116 import logging |
|
52526
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
117 |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
118 logger = get_logger() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
119 formatter = logging.Formatter(DEFAULT_LOGGING_FORMAT) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
120 handler = logging.StreamHandler() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
121 handler.setFormatter(formatter) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
122 logger.addHandler(handler) |
|
52526
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
123 |
|
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
124 if level: |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
125 logger.setLevel(level) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
126 _log_to_stderr = True |
|
52526
05f90cecb2a2
Merged revisions 68915 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents:
52148
diff
changeset
|
127 return _logger |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
128 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
129 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
130 # Function returning a temp directory which will be removed on exit |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
131 # |
|
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 get_temp_dir(): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
134 # get name of a temp directory which will be automatically cleaned up |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
135 if current_process()._tempdir is None: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
136 import shutil, tempfile |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
137 tempdir = tempfile.mkdtemp(prefix='pymp-') |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
138 info('created temp directory %s', tempdir) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
139 Finalize(None, shutil.rmtree, args=[tempdir], exitpriority=-100) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
140 current_process()._tempdir = tempdir |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
141 return current_process()._tempdir |
|
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 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
144 # Support for reinitialization of objects when bootstrapping a child process |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
145 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
146 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
147 _afterfork_registry = weakref.WeakValueDictionary() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
148 _afterfork_counter = itertools.count() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
149 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
150 def _run_after_forkers(): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
151 items = list(_afterfork_registry.items()) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
152 items.sort() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
153 for (index, ident, func), obj in items: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
154 try: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
155 func(obj) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
156 except Exception, e: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
157 info('after forker raised exception %s', e) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
158 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
159 def register_after_fork(obj, func): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
160 _afterfork_registry[(_afterfork_counter.next(), id(obj), func)] = obj |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
161 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
162 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
163 # Finalization using weakrefs |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
164 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
165 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
166 _finalizer_registry = {} |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
167 _finalizer_counter = itertools.count() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
168 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
169 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
170 class Finalize(object): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
171 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
172 Class which supports object finalization using weakrefs |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
173 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
174 def __init__(self, obj, callback, args=(), kwargs=None, exitpriority=None): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
175 assert exitpriority is None or type(exitpriority) is int |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
176 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
177 if obj is not None: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
178 self._weakref = weakref.ref(obj, self) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
179 else: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
180 assert exitpriority is not None |
|
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 self._callback = callback |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
183 self._args = args |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
184 self._kwargs = kwargs or {} |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
185 self._key = (exitpriority, _finalizer_counter.next()) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
186 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
187 _finalizer_registry[self._key] = self |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
188 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
189 def __call__(self, wr=None): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
190 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
191 Run the callback unless it has already been called or cancelled |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
192 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
193 try: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
194 del _finalizer_registry[self._key] |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
195 except KeyError: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
196 sub_debug('finalizer no longer registered') |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
197 else: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
198 sub_debug('finalizer calling %s with args %s and kwargs %s', |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
199 self._callback, self._args, self._kwargs) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
200 res = self._callback(*self._args, **self._kwargs) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
201 self._weakref = self._callback = self._args = \ |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
202 self._kwargs = self._key = None |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
203 return res |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
204 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
205 def cancel(self): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
206 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
207 Cancel finalization of the object |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
208 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
209 try: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
210 del _finalizer_registry[self._key] |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
211 except KeyError: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
212 pass |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
213 else: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
214 self._weakref = self._callback = self._args = \ |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
215 self._kwargs = self._key = None |
|
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 still_active(self): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
218 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
219 Return whether this finalizer is still waiting to invoke callback |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
220 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
221 return self._key in _finalizer_registry |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
222 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
223 def __repr__(self): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
224 try: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
225 obj = self._weakref() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
226 except (AttributeError, TypeError): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
227 obj = None |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
228 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
229 if obj is None: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
230 return '<Finalize object, dead>' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
231 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
232 x = '<Finalize object, callback=%s' % \ |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
233 getattr(self._callback, '__name__', self._callback) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
234 if self._args: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
235 x += ', args=' + str(self._args) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
236 if self._kwargs: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
237 x += ', kwargs=' + str(self._kwargs) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
238 if self._key[0] is not None: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
239 x += ', exitprority=' + str(self._key[0]) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
240 return x + '>' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
241 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
242 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
243 def _run_finalizers(minpriority=None): |
|
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 Run all finalizers whose exit priority is not None and at least minpriority |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
246 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
247 Finalizers with highest priority are called first; finalizers with |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
248 the same priority will be called in reverse order of creation. |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
249 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
250 if minpriority is None: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
251 f = lambda p : p[0][0] is not None |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
252 else: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
253 f = lambda p : p[0][0] is not None and p[0][0] >= minpriority |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
254 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
255 items = [x for x in _finalizer_registry.items() if f(x)] |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
256 items.sort(reverse=True) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
257 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
258 for key, finalizer in items: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
259 sub_debug('calling %s', finalizer) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
260 try: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
261 finalizer() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
262 except Exception: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
263 import traceback |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
264 traceback.print_exc() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
265 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
266 if minpriority is None: |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
267 _finalizer_registry.clear() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
268 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
269 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
270 # Clean up on exit |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
271 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
272 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
273 def is_exiting(): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
274 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
275 Returns true if the process is shutting down |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
276 ''' |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
277 return _exiting or _exiting is None |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
278 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
279 _exiting = False |
|
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 def _exit_function(): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
282 global _exiting |
|
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 info('process shutting down') |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
285 debug('running all "atexit" finalizers with priority >= 0') |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
286 _run_finalizers(0) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
287 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
288 for p in active_children(): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
289 if p._daemonic: |
|
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:
47980
diff
changeset
|
290 info('calling terminate() for daemon %s', p.name) |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
291 p._popen.terminate() |
|
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 for p in active_children(): |
|
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:
47980
diff
changeset
|
294 info('calling join() for process %s', p.name) |
|
47696
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
295 p.join() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
296 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
297 debug('running the remaining "atexit" finalizers') |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
298 _run_finalizers() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
299 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
300 atexit.register(_exit_function) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
301 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
302 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
303 # Some fork aware types |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
304 # |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
305 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
306 class ForkAwareThreadLock(object): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
307 def __init__(self): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
308 self._lock = threading.Lock() |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
309 self.acquire = self._lock.acquire |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
310 self.release = self._lock.release |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
311 register_after_fork(self, ForkAwareThreadLock.__init__) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
312 |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
313 class ForkAwareLocal(threading.local): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
314 def __init__(self): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
315 register_after_fork(self, lambda obj : obj.__dict__.clear()) |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
316 def __reduce__(self): |
|
d01165fae98e
darn! I converted half of the files the wrong way.
Benjamin Peterson <benjamin@python.org>
parents:
47694
diff
changeset
|
317 return type(self), () |
