Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 1 | # |
| 2 | # Support for the API of the multiprocessing package using threads |
| 3 | # |
| 4 | # multiprocessing/dummy/__init__.py |
| 5 | # |
R. David Murray | 3fc969a | 2010-12-14 01:38:16 +0000 | [diff] [blame] | 6 | # Copyright (c) 2006-2008, R Oudkerk |
| 7 | # All rights reserved. |
| 8 | # |
| 9 | # Redistribution and use in source and binary forms, with or without |
| 10 | # modification, are permitted provided that the following conditions |
| 11 | # are met: |
| 12 | # |
| 13 | # 1. Redistributions of source code must retain the above copyright |
| 14 | # notice, this list of conditions and the following disclaimer. |
| 15 | # 2. Redistributions in binary form must reproduce the above copyright |
| 16 | # notice, this list of conditions and the following disclaimer in the |
| 17 | # documentation and/or other materials provided with the distribution. |
| 18 | # 3. Neither the name of author nor the names of any contributors may be |
| 19 | # used to endorse or promote products derived from this software |
| 20 | # without specific prior written permission. |
| 21 | # |
| 22 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND |
| 23 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 25 | # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 26 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 27 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 28 | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 29 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 30 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 31 | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 32 | # SUCH DAMAGE. |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 33 | # |
| 34 | |
| 35 | __all__ = [ |
| 36 | 'Process', 'current_process', 'active_children', 'freeze_support', |
| 37 | 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition', |
Richard Oudkerk | 3730a17 | 2012-06-15 18:26:07 +0100 | [diff] [blame] | 38 | 'Event', 'Barrier', 'Queue', 'Manager', 'Pipe', 'Pool', 'JoinableQueue' |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 39 | ] |
| 40 | |
| 41 | # |
| 42 | # Imports |
| 43 | # |
| 44 | |
| 45 | import threading |
| 46 | import sys |
| 47 | import weakref |
| 48 | import array |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 49 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 50 | from multiprocessing.dummy.connection import Pipe |
| 51 | from threading import Lock, RLock, Semaphore, BoundedSemaphore |
Richard Oudkerk | 3730a17 | 2012-06-15 18:26:07 +0100 | [diff] [blame] | 52 | from threading import Event, Condition, Barrier |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 53 | from queue import Queue |
| 54 | |
| 55 | # |
| 56 | # |
| 57 | # |
| 58 | |
| 59 | class DummyProcess(threading.Thread): |
| 60 | |
| 61 | def __init__(self, group=None, target=None, name=None, args=(), kwargs={}): |
| 62 | threading.Thread.__init__(self, group, target, name, args, kwargs) |
| 63 | self._pid = None |
| 64 | self._children = weakref.WeakKeyDictionary() |
| 65 | self._start_called = False |
| 66 | self._parent = current_process() |
| 67 | |
| 68 | def start(self): |
| 69 | assert self._parent is current_process() |
| 70 | self._start_called = True |
Richard Oudkerk | 54454e7 | 2012-05-25 12:57:58 +0100 | [diff] [blame] | 71 | if hasattr(self._parent, '_children'): |
| 72 | self._parent._children[self] = None |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 73 | threading.Thread.start(self) |
| 74 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 75 | @property |
| 76 | def exitcode(self): |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 77 | if self._start_called and not self.is_alive(): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 78 | return 0 |
| 79 | else: |
| 80 | return None |
| 81 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 82 | # |
| 83 | # |
| 84 | # |
| 85 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 86 | Process = DummyProcess |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 87 | current_process = threading.current_thread |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 88 | current_process()._children = weakref.WeakKeyDictionary() |
| 89 | |
| 90 | def active_children(): |
| 91 | children = current_process()._children |
| 92 | for p in list(children): |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 93 | if not p.is_alive(): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 94 | children.pop(p, None) |
| 95 | return list(children) |
| 96 | |
| 97 | def freeze_support(): |
| 98 | pass |
| 99 | |
| 100 | # |
| 101 | # |
| 102 | # |
| 103 | |
| 104 | class Namespace(object): |
| 105 | def __init__(self, **kwds): |
| 106 | self.__dict__.update(kwds) |
| 107 | def __repr__(self): |
| 108 | items = list(self.__dict__.items()) |
| 109 | temp = [] |
| 110 | for name, value in items: |
| 111 | if not name.startswith('_'): |
| 112 | temp.append('%s=%r' % (name, value)) |
| 113 | temp.sort() |
| 114 | return 'Namespace(%s)' % str.join(', ', temp) |
| 115 | |
| 116 | dict = dict |
| 117 | list = list |
| 118 | |
| 119 | def Array(typecode, sequence, lock=True): |
| 120 | return array.array(typecode, sequence) |
| 121 | |
| 122 | class Value(object): |
| 123 | def __init__(self, typecode, value, lock=True): |
| 124 | self._typecode = typecode |
| 125 | self._value = value |
| 126 | def _get(self): |
| 127 | return self._value |
| 128 | def _set(self, value): |
| 129 | self._value = value |
| 130 | value = property(_get, _set) |
| 131 | def __repr__(self): |
| 132 | return '<%r(%r, %r)>'%(type(self).__name__,self._typecode,self._value) |
| 133 | |
| 134 | def Manager(): |
| 135 | return sys.modules[__name__] |
| 136 | |
| 137 | def shutdown(): |
| 138 | pass |
| 139 | |
| 140 | def Pool(processes=None, initializer=None, initargs=()): |
| 141 | from multiprocessing.pool import ThreadPool |
| 142 | return ThreadPool(processes, initializer, initargs) |
| 143 | |
| 144 | JoinableQueue = Queue |