Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b15c025

Browse files
authored
Add multiprocessing.sharedctypes stubs (#5231)
1 parent 0670991 commit b15c025

3 files changed

Lines changed: 152 additions & 64 deletions

File tree

stdlib/multiprocessing/__init__.pyi

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
2+
from collections.abc import Callable, Iterable
23
from logging import Logger
3-
from multiprocessing import connection, pool, sharedctypes, synchronize
4+
from multiprocessing import connection, context, pool, synchronize
45
from multiprocessing.context import (
56
AuthenticationError as AuthenticationError,
67
BaseContext,
@@ -17,7 +18,7 @@ from multiprocessing.process import active_children as active_children, current_
1718
# These are technically functions that return instances of these Queue classes. See #4313 for discussion
1819
from multiprocessing.queues import JoinableQueue as JoinableQueue, Queue as Queue, SimpleQueue as SimpleQueue
1920
from multiprocessing.spawn import freeze_support as freeze_support
20-
from typing import Any, Callable, Iterable, List, Optional, Sequence, Tuple, Union, overload
21+
from typing import Any, Optional, Union, overload
2122
from typing_extensions import Literal
2223

2324
if sys.version_info >= (3, 8):
@@ -32,6 +33,10 @@ if sys.platform != "win32":
3233

3334
# Sychronization primitives
3435
_LockLike = Union[synchronize.Lock, synchronize.RLock]
36+
RawValue = context._default_context.RawValue
37+
RawArray = context._default_context.RawArray
38+
Value = context._default_context.Value
39+
Array = context._default_context.Array
3540

3641
def Barrier(parties: int, action: Optional[Callable[..., Any]] = ..., timeout: Optional[float] = ...) -> synchronize.Barrier: ...
3742
def BoundedSemaphore(value: int = ...) -> synchronize.BoundedSemaphore: ...
@@ -40,29 +45,23 @@ def Event() -> synchronize.Event: ...
4045
def Lock() -> synchronize.Lock: ...
4146
def RLock() -> synchronize.RLock: ...
4247
def Semaphore(value: int = ...) -> synchronize.Semaphore: ...
43-
def Pipe(duplex: bool = ...) -> Tuple[connection.Connection, connection.Connection]: ...
48+
def Pipe(duplex: bool = ...) -> tuple[connection.Connection, connection.Connection]: ...
4449
def Pool(
4550
processes: Optional[int] = ...,
4651
initializer: Optional[Callable[..., Any]] = ...,
4752
initargs: Iterable[Any] = ...,
4853
maxtasksperchild: Optional[int] = ...,
4954
) -> pool.Pool: ...
5055

51-
# Functions Array and Value are copied from context.pyi.
52-
# See https://github.com/python/typeshed/blob/ac234f25927634e06d9c96df98d72d54dd80dfc4/stdlib/2and3/turtle.pyi#L284-L291
53-
# for rationale
54-
def Array(typecode_or_type: Any, size_or_initializer: Union[int, Sequence[Any]], *, lock: bool = ...) -> sharedctypes._Array: ...
55-
def Value(typecode_or_type: Any, *args: Any, lock: bool = ...) -> sharedctypes._Value: ...
56-
5756
# ----- multiprocessing function stubs -----
5857
def allow_connection_pickling() -> None: ...
5958
def cpu_count() -> int: ...
6059
def get_logger() -> Logger: ...
6160
def log_to_stderr(level: Optional[Union[str, int]] = ...) -> Logger: ...
6261
def Manager() -> SyncManager: ...
6362
def set_executable(executable: str) -> None: ...
64-
def set_forkserver_preload(module_names: List[str]) -> None: ...
65-
def get_all_start_methods() -> List[str]: ...
63+
def set_forkserver_preload(module_names: list[str]) -> None: ...
64+
def get_all_start_methods() -> list[str]: ...
6665
def get_start_method(allow_none: bool = ...) -> Optional[str]: ...
6766
def set_start_method(method: str, force: Optional[bool] = ...) -> None: ...
6867

stdlib/multiprocessing/context.pyi

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
import ctypes
12
import multiprocessing
23
import sys
4+
from collections.abc import Callable, Iterable, Sequence
5+
from ctypes import _CData
36
from logging import Logger
4-
from multiprocessing import queues, sharedctypes, synchronize
7+
from multiprocessing import queues, synchronize
58
from multiprocessing.process import BaseProcess
6-
from typing import Any, Callable, Iterable, List, Optional, Sequence, Type, Union, overload
9+
from multiprocessing.sharedctypes import SynchronizedArray, SynchronizedBase
10+
from typing import Any, Optional, Type, TypeVar, Union, overload
711
from typing_extensions import Literal
812

913
_LockLike = Union[synchronize.Lock, synchronize.RLock]
14+
_CT = TypeVar("_CT", bound=_CData)
1015

1116
class ProcessError(Exception): ...
1217
class BufferTooShort(ProcessError): ...
@@ -28,7 +33,7 @@ class BaseContext(object):
2833
@staticmethod
2934
def parent_process() -> Optional[BaseProcess]: ...
3035
@staticmethod
31-
def active_children() -> List[BaseProcess]: ...
36+
def active_children() -> list[BaseProcess]: ...
3237
def cpu_count(self) -> int: ...
3338
# TODO: change return to SyncManager once a stub exists in multiprocessing.managers
3439
def Manager(self) -> Any: ...
@@ -53,28 +58,52 @@ class BaseContext(object):
5358
initargs: Iterable[Any] = ...,
5459
maxtasksperchild: Optional[int] = ...,
5560
) -> multiprocessing.pool.Pool: ...
56-
# TODO: typecode_or_type param is a ctype with a base class of _SimpleCData or array.typecode Need to figure out
57-
# how to handle the ctype
58-
# TODO: change return to RawValue once a stub exists in multiprocessing.sharedctypes
59-
def RawValue(self, typecode_or_type: Any, *args: Any) -> Any: ...
60-
# TODO: typecode_or_type param is a ctype with a base class of _SimpleCData or array.typecode Need to figure out
61-
# how to handle the ctype
62-
# TODO: change return to RawArray once a stub exists in multiprocessing.sharedctypes
63-
def RawArray(self, typecode_or_type: Any, size_or_initializer: Union[int, Sequence[Any]]) -> Any: ...
64-
# TODO: typecode_or_type param is a ctype with a base class of _SimpleCData or array.typecode Need to figure out
65-
# how to handle the ctype
66-
def Value(self, typecode_or_type: Any, *args: Any, lock: bool = ...) -> sharedctypes._Value: ...
67-
# TODO: typecode_or_type param is a ctype with a base class of _SimpleCData or array.typecode Need to figure out
68-
# how to handle the ctype
61+
@overload
62+
def RawValue(self, typecode_or_type: Type[_CT], *args: Any) -> _CT: ...
63+
@overload
64+
def RawValue(self, typecode_or_type: str, *args: Any) -> Any: ...
65+
@overload
66+
def RawArray(self, typecode_or_type: Type[_CT], size_or_initializer: Union[int, Sequence[Any]]) -> ctypes.Array[_CT]: ...
67+
@overload
68+
def RawArray(self, typecode_or_type: str, size_or_initializer: Union[int, Sequence[Any]]) -> Any: ...
69+
@overload
70+
def Value(self, typecode_or_type: Type[_CT], *args: Any, lock: Literal[False]) -> _CT: ...
71+
@overload
72+
def Value(self, typecode_or_type: Type[_CT], *args: Any, lock: Union[Literal[True], _LockLike]) -> SynchronizedBase[_CT]: ...
73+
@overload
74+
def Value(self, typecode_or_type: str, *args: Any, lock: Union[Literal[True], _LockLike]) -> SynchronizedBase[Any]: ...
75+
@overload
76+
def Value(self, typecode_or_type: Union[str, Type[_CData]], *args: Any, lock: Union[bool, _LockLike] = ...) -> Any: ...
77+
@overload
6978
def Array(
70-
self, typecode_or_type: Any, size_or_initializer: Union[int, Sequence[Any]], *, lock: bool = ...
71-
) -> sharedctypes._Array: ...
79+
self, typecode_or_type: Type[_CT], size_or_initializer: Union[int, Sequence[Any]], *, lock: Literal[False]
80+
) -> _CT: ...
81+
@overload
82+
def Array(
83+
self,
84+
typecode_or_type: Type[_CT],
85+
size_or_initializer: Union[int, Sequence[Any]],
86+
*,
87+
lock: Union[Literal[True], _LockLike],
88+
) -> SynchronizedArray[_CT]: ...
89+
@overload
90+
def Array(
91+
self, typecode_or_type: str, size_or_initializer: Union[int, Sequence[Any]], *, lock: Union[Literal[True], _LockLike]
92+
) -> SynchronizedArray[Any]: ...
93+
@overload
94+
def Array(
95+
self,
96+
typecode_or_type: Union[str, Type[_CData]],
97+
size_or_initializer: Union[int, Sequence[Any]],
98+
*,
99+
lock: Union[bool, _LockLike] = ...,
100+
) -> Any: ...
72101
def freeze_support(self) -> None: ...
73102
def get_logger(self) -> Logger: ...
74103
def log_to_stderr(self, level: Optional[str] = ...) -> Logger: ...
75104
def allow_connection_pickling(self) -> None: ...
76105
def set_executable(self, executable: str) -> None: ...
77-
def set_forkserver_preload(self, module_names: List[str]) -> None: ...
106+
def set_forkserver_preload(self, module_names: list[str]) -> None: ...
78107
if sys.platform != "win32":
79108
@overload
80109
def get_context(self, method: None = ...) -> DefaultContext: ...
@@ -111,7 +140,9 @@ class DefaultContext(BaseContext):
111140
def __init__(self, context: BaseContext) -> None: ...
112141
def set_start_method(self, method: Optional[str], force: bool = ...) -> None: ...
113142
def get_start_method(self, allow_none: bool = ...) -> str: ...
114-
def get_all_start_methods(self) -> List[str]: ...
143+
def get_all_start_methods(self) -> list[str]: ...
144+
145+
_default_context: DefaultContext
115146

116147
if sys.platform != "win32":
117148
class ForkProcess(BaseProcess):
Lines changed: 91 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,101 @@
1-
from ctypes import _CData
1+
import ctypes
2+
from collections.abc import Callable, Iterable, Sequence
3+
from ctypes import _CData, _SimpleCData, c_char
24
from multiprocessing.context import BaseContext
35
from multiprocessing.synchronize import _LockLike
4-
from typing import Any, List, Optional, Sequence, Type, Union, overload
6+
from typing import Any, Generic, Optional, Protocol, Type, TypeVar, Union, overload
7+
from typing_extensions import Literal
58

6-
class _Array:
7-
value: Any = ...
8-
def __init__(
9-
self,
10-
typecode_or_type: Union[str, Type[_CData]],
11-
size_or_initializer: Union[int, Sequence[Any]],
12-
*,
13-
lock: Union[bool, _LockLike] = ...,
14-
) -> None: ...
15-
def acquire(self) -> bool: ...
16-
def release(self) -> bool: ...
17-
def get_lock(self) -> _LockLike: ...
18-
def get_obj(self) -> Any: ...
19-
@overload
20-
def __getitem__(self, key: int) -> Any: ...
21-
@overload
22-
def __getitem__(self, key: slice) -> List[Any]: ...
23-
def __getslice__(self, start: int, stop: int) -> Any: ...
24-
def __setitem__(self, key: int, value: Any) -> None: ...
25-
26-
class _Value:
27-
value: Any = ...
28-
def __init__(self, typecode_or_type: Union[str, Type[_CData]], *args: Any, lock: Union[bool, _LockLike] = ...) -> None: ...
29-
def get_lock(self) -> _LockLike: ...
30-
def get_obj(self) -> Any: ...
31-
def acquire(self) -> bool: ...
32-
def release(self) -> bool: ...
9+
_T = TypeVar("_T")
10+
_CT = TypeVar("_CT", bound=_CData)
3311

12+
@overload
13+
def RawValue(typecode_or_type: Type[_CT], *args: Any) -> _CT: ...
14+
@overload
15+
def RawValue(typecode_or_type: str, *args: Any) -> Any: ...
16+
@overload
17+
def RawArray(typecode_or_type: Type[_CT], size_or_initializer: Union[int, Sequence[Any]]) -> ctypes.Array[_CT]: ...
18+
@overload
19+
def RawArray(typecode_or_type: str, size_or_initializer: Union[int, Sequence[Any]]) -> Any: ...
20+
@overload
21+
def Value(typecode_or_type: Type[_CT], *args: Any, lock: Literal[False], ctx: Optional[BaseContext] = ...) -> _CT: ...
22+
@overload
23+
def Value(
24+
typecode_or_type: Type[_CT], *args: Any, lock: Union[Literal[True], _LockLike], ctx: Optional[BaseContext] = ...
25+
) -> SynchronizedBase[_CT]: ...
26+
@overload
27+
def Value(
28+
typecode_or_type: str, *args: Any, lock: Union[Literal[True], _LockLike], ctx: Optional[BaseContext] = ...
29+
) -> SynchronizedBase[Any]: ...
30+
@overload
31+
def Value(
32+
typecode_or_type: Union[str, Type[_CData]], *args: Any, lock: Union[bool, _LockLike] = ..., ctx: Optional[BaseContext] = ...
33+
) -> Any: ...
34+
@overload
35+
def Array(
36+
typecode_or_type: Type[_CT],
37+
size_or_initializer: Union[int, Sequence[Any]],
38+
*,
39+
lock: Literal[False],
40+
ctx: Optional[BaseContext] = ...,
41+
) -> _CT: ...
42+
@overload
43+
def Array(
44+
typecode_or_type: Type[_CT],
45+
size_or_initializer: Union[int, Sequence[Any]],
46+
*,
47+
lock: Union[Literal[True], _LockLike],
48+
ctx: Optional[BaseContext] = ...,
49+
) -> SynchronizedArray[_CT]: ...
50+
@overload
51+
def Array(
52+
typecode_or_type: str,
53+
size_or_initializer: Union[int, Sequence[Any]],
54+
*,
55+
lock: Union[Literal[True], _LockLike],
56+
ctx: Optional[BaseContext] = ...,
57+
) -> SynchronizedArray[Any]: ...
58+
@overload
3459
def Array(
3560
typecode_or_type: Union[str, Type[_CData]],
3661
size_or_initializer: Union[int, Sequence[Any]],
3762
*,
3863
lock: Union[bool, _LockLike] = ...,
3964
ctx: Optional[BaseContext] = ...,
40-
) -> _Array: ...
41-
def Value(
42-
typecode_or_type: Union[str, Type[_CData]], *args: Any, lock: Union[bool, _LockLike] = ..., ctx: Optional[BaseContext] = ...
43-
) -> _Value: ...
65+
) -> Any: ...
66+
def copy(obj: _CT) -> _CT: ...
67+
@overload
68+
def synchronized(obj: _SimpleCData[_T], lock: Optional[_LockLike] = ..., ctx: Optional[Any] = ...) -> Synchronized[_T]: ...
69+
@overload
70+
def synchronized(obj: ctypes.Array[c_char], lock: Optional[_LockLike] = ..., ctx: Optional[Any] = ...) -> SynchronizedString: ...
71+
@overload
72+
def synchronized(obj: ctypes.Array[_CT], lock: Optional[_LockLike] = ..., ctx: Optional[Any] = ...) -> SynchronizedArray[_CT]: ...
73+
@overload
74+
def synchronized(obj: _CT, lock: Optional[_LockLike] = ..., ctx: Optional[Any] = ...) -> SynchronizedBase[_CT]: ...
75+
76+
class _AcquireFunc(Protocol):
77+
def __call__(self, block: bool = ..., timeout: Optional[float] = ...) -> bool: ...
78+
79+
class SynchronizedBase(Generic[_CT]):
80+
acquire: _AcquireFunc = ...
81+
release: Callable[[], None] = ...
82+
def __init__(self, obj: Any, lock: Optional[_LockLike] = ..., ctx: Optional[Any] = ...) -> None: ...
83+
def __reduce__(self) -> tuple[Callable[..., Any], tuple[Any, _LockLike]]: ...
84+
def get_obj(self) -> _CT: ...
85+
def get_lock(self) -> _LockLike: ...
86+
def __enter__(self) -> bool: ...
87+
def __exit__(self, *args: Any) -> None: ...
88+
89+
class Synchronized(SynchronizedBase[_SimpleCData[_T]], Generic[_T]):
90+
value: _T
91+
92+
class SynchronizedArray(SynchronizedBase[ctypes.Array[_CT]], Generic[_CT]):
93+
def __len__(self) -> int: ...
94+
def __getitem__(self, i: int) -> _CT: ...
95+
def __setitem__(self, i: int, o: _CT) -> None: ...
96+
def __getslice__(self, start: int, stop: int) -> list[_CT]: ...
97+
def __setslice__(self, start: int, stop: int, values: Iterable[_CT]) -> None: ...
98+
99+
class SynchronizedString(SynchronizedArray[c_char]):
100+
value: bytes
101+
raw: bytes

0 commit comments

Comments
 (0)