__init__.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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
  7. # Licensed to PSF under a Contributor Agreement.
  8. #
  9. __all__ = [
  10. 'Process', 'current_process', 'active_children', 'freeze_support',
  11. 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition',
  12. 'Event', 'Barrier', 'Queue', 'Manager', 'Pipe', 'Pool', 'JoinableQueue'
  13. ]
  14. #
  15. # Imports
  16. #
  17. import threading
  18. import sys
  19. import weakref
  20. import array
  21. from .connection import Pipe
  22. from threading import Lock, RLock, Semaphore, BoundedSemaphore
  23. from threading import Event, Condition, Barrier
  24. from queue import Queue
  25. #
  26. #
  27. #
  28. class DummyProcess(threading.Thread):
  29. def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):
  30. threading.Thread.__init__(self, group, target, name, args, kwargs)
  31. self._pid = None
  32. self._children = weakref.WeakKeyDictionary()
  33. self._start_called = False
  34. self._parent = current_process()
  35. def start(self):
  36. if self._parent is not current_process():
  37. raise RuntimeError(
  38. "Parent is {0!r} but current_process is {1!r}".format(
  39. self._parent, current_process()))
  40. self._start_called = True
  41. if hasattr(self._parent, '_children'):
  42. self._parent._children[self] = None
  43. threading.Thread.start(self)
  44. @property
  45. def exitcode(self):
  46. if self._start_called and not self.is_alive():
  47. return 0
  48. else:
  49. return None
  50. #
  51. #
  52. #
  53. Process = DummyProcess
  54. current_process = threading.current_thread
  55. current_process()._children = weakref.WeakKeyDictionary()
  56. def active_children():
  57. children = current_process()._children
  58. for p in list(children):
  59. if not p.is_alive():
  60. children.pop(p, None)
  61. return list(children)
  62. def freeze_support():
  63. pass
  64. #
  65. #
  66. #
  67. class Namespace(object):
  68. def __init__(self, /, **kwds):
  69. self.__dict__.update(kwds)
  70. def __repr__(self):
  71. items = list(self.__dict__.items())
  72. temp = []
  73. for name, value in items:
  74. if not name.startswith('_'):
  75. temp.append('%s=%r' % (name, value))
  76. temp.sort()
  77. return '%s(%s)' % (self.__class__.__name__, ', '.join(temp))
  78. dict = dict
  79. list = list
  80. def Array(typecode, sequence, lock=True):
  81. return array.array(typecode, sequence)
  82. class Value(object):
  83. def __init__(self, typecode, value, lock=True):
  84. self._typecode = typecode
  85. self._value = value
  86. @property
  87. def value(self):
  88. return self._value
  89. @value.setter
  90. def value(self, value):
  91. self._value = value
  92. def __repr__(self):
  93. return '<%s(%r, %r)>'%(type(self).__name__,self._typecode,self._value)
  94. def Manager():
  95. return sys.modules[__name__]
  96. def shutdown():
  97. pass
  98. def Pool(processes=None, initializer=None, initargs=()):
  99. from ..pool import ThreadPool
  100. return ThreadPool(processes, initializer, initargs)
  101. JoinableQueue = Queue