| 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 |
|
| 12 |
|
| 13 |
class LokyProcess(BaseProcess): |
| 14 |
_start_method = "loky" |
| 15 |
|
| 16 |
def __init__( |
| 17 |
self, |
| 18 |
group=None, |
| 19 |
target=None, |
| 20 |
name=None, |
| 21 |
args=(), |
| 22 |
kwargs={}, |
| 23 |
daemon=None, |
| 24 |
init_main_module=False, |
| 25 |
env=None, |
| 26 |
): |
| 27 |
super().__init__( |
| 28 |
group=group, |
| 29 |
target=target, |
| 30 |
name=name, |
| 31 |
args=args, |
| 32 |
kwargs=kwargs, |
| 33 |
daemon=daemon, |
| 34 |
) |
| 35 |
self.env = {} if env is None else env |
| 36 |
self.authkey = self.authkey |
| 37 |
self.init_main_module = init_main_module |
| 38 |
|
| 39 |
@staticmethod |
| 40 |
def _Popen(process_obj): |
| 41 |
if sys.platform == "win32": |
| 42 |
from .popen_loky_win32 import Popen |
| 43 |
else: |
| 44 |
from .popen_loky_posix import Popen |
| 45 |
return Popen(process_obj) |
| 46 |
|
| 47 |
|
| 48 |
class LokyInitMainProcess(LokyProcess): |
| 49 |
_start_method = "loky_init_main" |
| 50 |
|
| 51 |
def __init__( |
| 52 |
self, |
| 53 |
group=None, |
| 54 |
target=None, |
| 55 |
name=None, |
| 56 |
args=(), |
| 57 |
kwargs={}, |
| 58 |
daemon=None, |
| 59 |
): |
| 60 |
super().__init__( |
| 61 |
group=group, |
| 62 |
target=target, |
| 63 |
name=name, |
| 64 |
args=args, |
| 65 |
kwargs=kwargs, |
| 66 |
daemon=daemon, |
| 67 |
init_main_module=True, |
| 68 |
) |
| 69 |
|
| 70 |
|
| 71 |
# |
| 72 |
# We subclass bytes to avoid accidental transmission of auth keys over network |
| 73 |
# |
| 74 |
|
| 75 |
|
| 76 |
class AuthenticationKey(bytes): |
| 77 |
def __reduce__(self): |
| 78 |
try: |
| 79 |
assert_spawning(self) |
| 80 |
except RuntimeError: |
| 81 |
raise TypeError( |
| 82 |
"Pickling an AuthenticationKey object is " |
| 83 |
"disallowed for security reasons" |
| 84 |
) |
| 85 |
return AuthenticationKey, (bytes(self),) |