|
11 | 11 |
|
12 | 12 | # Import packages
|
13 | 13 | import os
|
| 14 | +import multiprocessing as mp |
14 | 15 | from multiprocessing import Pool, cpu_count, pool
|
15 | 16 | from traceback import format_exception
|
16 | 17 | import sys
|
@@ -85,15 +86,53 @@ def daemon(self):
|
85 | 86 | def daemon(self, val):
|
86 | 87 | pass
|
87 | 88 |
|
| 89 | +try: |
| 90 | + from multiprocessing import context |
| 91 | + # Exists on all platforms |
| 92 | + class NonDaemonSpawnProcess(NonDaemonMixin, context.SpawnProcess): |
| 93 | + pass |
| 94 | + class NonDaemonSpawnContext(context.SpawnContext): |
| 95 | + Process = NonDaemonSpawnProcess |
| 96 | + _nondaemon_context_mapper = { |
| 97 | + 'spawn': NonDaemonSpawnContext() |
| 98 | + } |
88 | 99 |
|
89 |
| -class NonDaemonPool(pool.Pool): |
90 |
| - """A process pool with non-daemon processes. |
91 |
| - """ |
92 |
| - def Process(self, *args, **kwds): |
93 |
| - proc = super(NonDaemonPool, self).Process(*args, **kwds) |
94 |
| - # Monkey-patch newly created processes to ensure they are never daemonized |
95 |
| - proc.__class__ = type(str('NonDaemonProcess'), (NonDaemonMixin, proc.__class__), {}) |
96 |
| - return proc |
| 100 | + # POSIX only |
| 101 | + try: |
| 102 | + class NonDaemonForkProcess(NonDaemonMixin, context.ForkProcess): |
| 103 | + pass |
| 104 | + class NonDaemonForkContext(context.ForkContext): |
| 105 | + Process = NonDaemonForkProcess |
| 106 | + _nondaemon_context_mapper['fork'] = NonDaemonForkContext() |
| 107 | + except AttributeError: |
| 108 | + pass |
| 109 | + # POSIX only |
| 110 | + try: |
| 111 | + class NonDaemonForkServerProcess(NonDaemonMixin, context.ForkServerProcess): |
| 112 | + pass |
| 113 | + class NonDaemonForkServerContext(context.ForkServerContext): |
| 114 | + Process = NonDaemonForkServerProcess |
| 115 | + _nondaemon_context_mapper['forkserver'] = NonDaemonForkServerContext() |
| 116 | + except AttributeError: |
| 117 | + pass |
| 118 | + |
| 119 | + class NonDaemonPool(pool.Pool): |
| 120 | + def __init__(self, processes=None, initializer=None, initargs=(), |
| 121 | + maxtasksperchild=None, context=None): |
| 122 | + if context is None: |
| 123 | + context = mp.get_context() |
| 124 | + context = _nondaemon_context_mapper[context._name] |
| 125 | + super(NonDaemonPool, self).__init__(processes=processes, |
| 126 | + initializer=initializer, |
| 127 | + initargs=initargs, |
| 128 | + maxtasksperchild=maxtasksperchild, |
| 129 | + context=context) |
| 130 | + |
| 131 | +except ImportError: |
| 132 | + class NonDaemonProcess(NonDaemonMixin, mp.Process): |
| 133 | + pass |
| 134 | + class NonDaemonPool(pool.Pool): |
| 135 | + Process = NonDaemonProcess |
97 | 136 |
|
98 | 137 |
|
99 | 138 | class LegacyMultiProcPlugin(DistributedPluginBase):
|
|
0 commit comments