/Lib/multiprocessing/dummy/__init__.py

http://unladen-swallow.googlecode.com/ · Python · 126 lines · 80 code · 24 blank · 22 comment · 7 complexity · 03a2d55c393f42ddb94d0221e002ec3e MD5 · raw file

  1. #
  2. # Support for the API of the multiprocessing package using threads
  3. #
  4. # multiprocessing/dummy/__init__.py
  5. #
  6. # Copyright (c) 2006-2008, R Oudkerk --- see COPYING.txt
  7. #
  8. __all__ = [
  9. 'Process', 'current_process', 'active_children', 'freeze_support',
  10. 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition',
  11. 'Event', 'Queue', 'Manager', 'Pipe', 'Pool', 'JoinableQueue'
  12. ]
  13. #
  14. # Imports
  15. #
  16. import threading
  17. import sys
  18. import weakref
  19. import array
  20. import itertools
  21. from multiprocessing import TimeoutError, cpu_count
  22. from multiprocessing.dummy.connection import Pipe
  23. from threading import Lock, RLock, Semaphore, BoundedSemaphore
  24. from threading import Event
  25. from Queue import Queue
  26. #
  27. #
  28. #
  29. class DummyProcess(threading.Thread):
  30. def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):
  31. threading.Thread.__init__(self, group, target, name, args, kwargs)
  32. self._pid = None
  33. self._children = weakref.WeakKeyDictionary()
  34. self._start_called = False
  35. self._parent = current_process()
  36. def start(self):
  37. assert self._parent is current_process()
  38. self._start_called = True
  39. self._parent._children[self] = None
  40. threading.Thread.start(self)
  41. @property
  42. def exitcode(self):
  43. if self._start_called and not self.is_alive():
  44. return 0
  45. else:
  46. return None
  47. #
  48. #
  49. #
  50. class Condition(threading._Condition):
  51. notify_all = threading._Condition.notify_all.im_func
  52. #
  53. #
  54. #
  55. Process = DummyProcess
  56. current_process = threading.current_thread
  57. current_process()._children = weakref.WeakKeyDictionary()
  58. def active_children():
  59. children = current_process()._children
  60. for p in list(children):
  61. if not p.is_alive():
  62. children.pop(p, None)
  63. return list(children)
  64. def freeze_support():
  65. pass
  66. #
  67. #
  68. #
  69. class Namespace(object):
  70. def __init__(self, **kwds):
  71. self.__dict__.update(kwds)
  72. def __repr__(self):
  73. items = self.__dict__.items()
  74. temp = []
  75. for name, value in items:
  76. if not name.startswith('_'):
  77. temp.append('%s=%r' % (name, value))
  78. temp.sort()
  79. return 'Namespace(%s)' % str.join(', ', temp)
  80. dict = dict
  81. list = list
  82. def Array(typecode, sequence, lock=True):
  83. return array.array(typecode, sequence)
  84. class Value(object):
  85. def __init__(self, typecode, value, lock=True):
  86. self._typecode = typecode
  87. self._value = value
  88. def _get(self):
  89. return self._value
  90. def _set(self, value):
  91. self._value = value
  92. value = property(_get, _set)
  93. def __repr__(self):
  94. return '<%r(%r, %r)>'%(type(self).__name__,self._typecode,self._value)
  95. def Manager():
  96. return sys.modules[__name__]
  97. def shutdown():
  98. pass
  99. def Pool(processes=None, initializer=None, initargs=()):
  100. from multiprocessing.pool import ThreadPool
  101. return ThreadPool(processes, initializer, initargs)
  102. JoinableQueue = Queue