process.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. ###############################################################################
  2. # LokyProcess implementation
  3. #
  4. # authors: Thomas Moreau and Olivier Grisel
  5. #
  6. # based on multiprocessing/process.py (17/02/2017)
  7. #
  8. import sys
  9. from multiprocessing.context import assert_spawning
  10. from multiprocessing.process import BaseProcess
  11. class LokyProcess(BaseProcess):
  12. _start_method = "loky"
  13. def __init__(
  14. self,
  15. group=None,
  16. target=None,
  17. name=None,
  18. args=(),
  19. kwargs={},
  20. daemon=None,
  21. init_main_module=False,
  22. env=None,
  23. ):
  24. super().__init__(
  25. group=group,
  26. target=target,
  27. name=name,
  28. args=args,
  29. kwargs=kwargs,
  30. daemon=daemon,
  31. )
  32. self.env = {} if env is None else env
  33. self.authkey = self.authkey
  34. self.init_main_module = init_main_module
  35. @staticmethod
  36. def _Popen(process_obj):
  37. if sys.platform == "win32":
  38. from .popen_loky_win32 import Popen
  39. else:
  40. from .popen_loky_posix import Popen
  41. return Popen(process_obj)
  42. class LokyInitMainProcess(LokyProcess):
  43. _start_method = "loky_init_main"
  44. def __init__(
  45. self,
  46. group=None,
  47. target=None,
  48. name=None,
  49. args=(),
  50. kwargs={},
  51. daemon=None,
  52. ):
  53. super().__init__(
  54. group=group,
  55. target=target,
  56. name=name,
  57. args=args,
  58. kwargs=kwargs,
  59. daemon=daemon,
  60. init_main_module=True,
  61. )
  62. #
  63. # We subclass bytes to avoid accidental transmission of auth keys over network
  64. #
  65. class AuthenticationKey(bytes):
  66. def __reduce__(self):
  67. try:
  68. assert_spawning(self)
  69. except RuntimeError:
  70. raise TypeError(
  71. "Pickling an AuthenticationKey object is "
  72. "disallowed for security reasons"
  73. )
  74. return AuthenticationKey, (bytes(self),)