4
4
import asyncio
5
5
import logging
6
6
import multiprocessing
7
+ import multiprocessing .context
7
8
import multiprocessing .managers
8
9
import os
9
10
import sys
16
17
# shared context for all multiprocessing primitives, for platform compatibility
17
18
# "spawn" is default/required on windows and mac, but can't execute non-global functions
18
19
# see https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods
19
- context = multiprocessing .get_context (DEFAULT_START_METHOD )
20
+ context : multiprocessing .context .BaseContext = multiprocessing .get_context (
21
+ DEFAULT_START_METHOD
22
+ )
20
23
_manager = None
21
24
22
25
log = logging .getLogger (__name__ )
@@ -80,12 +83,12 @@ class Process:
80
83
def __init__ (
81
84
self ,
82
85
group : None = None ,
83
- target : Callable = None ,
84
- name : str = None ,
85
- args : Sequence [Any ] = None ,
86
- kwargs : Dict [str , Any ] = None ,
86
+ target : Optional [ Callable ] = None ,
87
+ name : Optional [ str ] = None ,
88
+ args : Optional [ Sequence [Any ] ] = None ,
89
+ kwargs : Optional [ Dict [str , Any ] ] = None ,
87
90
* ,
88
- daemon : bool = None ,
91
+ daemon : Optional [ bool ] = None ,
89
92
initializer : Optional [Callable ] = None ,
90
93
initargs : Sequence [Any ] = (),
91
94
loop_initializer : Optional [Callable ] = None ,
@@ -111,7 +114,7 @@ def __init__(
111
114
initargs = initargs ,
112
115
loop_initializer = loop_initializer ,
113
116
)
114
- self .aio_process = context .Process (
117
+ self .aio_process = context .Process ( # type: ignore[attr-defined]
115
118
group = group ,
116
119
target = process_target or Process .run_async ,
117
120
args = (self .unit ,),
@@ -127,7 +130,7 @@ def __await__(self) -> Any:
127
130
return self .join ().__await__ ()
128
131
129
132
@staticmethod
130
- def run_async (unit : Unit ) -> R :
133
+ def run_async (unit : Unit ) -> R : # type: ignore[type-var]
131
134
"""Initialize the child process and event loop, then execute the coroutine."""
132
135
try :
133
136
if unit .loop_initializer is None :
@@ -152,7 +155,7 @@ def start(self) -> None:
152
155
"""Start the child process."""
153
156
return self .aio_process .start ()
154
157
155
- async def join (self , timeout : int = None ) -> None :
158
+ async def join (self , timeout : Optional [ int ] = None ) -> None :
156
159
"""Wait for the process to finish execution without blocking the main thread."""
157
160
if not self .is_alive () and self .exitcode is None :
158
161
raise ValueError ("must start process before joining it" )
@@ -216,7 +219,7 @@ def __init__(self, *args, **kwargs) -> None:
216
219
self .unit .namespace .result = None
217
220
218
221
@staticmethod
219
- def run_async (unit : Unit ) -> R :
222
+ def run_async (unit : Unit ) -> R : # type: ignore[type-var]
220
223
"""Initialize the child process and event loop, then execute the coroutine."""
221
224
try :
222
225
result : R = Process .run_async (unit )
@@ -227,13 +230,13 @@ def run_async(unit: Unit) -> R:
227
230
unit .namespace .result = e
228
231
raise
229
232
230
- async def join (self , timeout : int = None ) -> Any :
233
+ async def join (self , timeout : Optional [ int ] = None ) -> Any :
231
234
"""Wait for the worker to finish, and return the final result."""
232
235
await super ().join (timeout )
233
236
return self .result
234
237
235
238
@property
236
- def result (self ) -> R :
239
+ def result (self ) -> R : # type: ignore[type-var]
237
240
"""Easy access to the resulting value from the coroutine."""
238
241
if self .exitcode is None :
239
242
raise ValueError ("coroutine not completed" )
0 commit comments