Coverage for faststream/_internal/cli/supervisors/asgi_multiprocess.py: 27%

37 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-17 21:20 +0000

1import inspect 

2from typing import TYPE_CHECKING, Any 

3 

4from faststream._internal._compat import HAS_UVICORN, uvicorn 

5from faststream.asgi.app import cast_uvicorn_params 

6from faststream.exceptions import INSTALL_UVICORN 

7 

8if TYPE_CHECKING: 

9 from faststream._internal.basic_types import SettingField 

10 from faststream._internal.cli.dto import RunArgs 

11 

12if HAS_UVICORN: 

13 from uvicorn.supervisors.multiprocess import Multiprocess, Process 

14 

15 # uvicorn 0.51 removed the `target` argument from Multiprocess and Process: 

16 # Process now builds its own Server from the config. Drop the legacy branches 

17 # once the supported uvicorn floor reaches 0.51. 

18 _UVICORN_TAKES_TARGET = ( 

19 "target" in inspect.signature(Multiprocess.__init__).parameters 

20 ) 

21 

22 class UvicornExtraConfig(uvicorn.Config): # type: ignore[misc] 

23 def __init__( 

24 self, 

25 run_extra_options: dict[str, "SettingField"], 

26 *args: Any, 

27 **kwargs: Any, 

28 ) -> None: 

29 super().__init__(*args, **kwargs) 

30 self._run_extra_options = run_extra_options 

31 

32 def load(self) -> None: 

33 super().load() 

34 self.loaded_app.app._run_extra_options = self._run_extra_options 

35 

36 class UvicornMultiprocess(Multiprocess): 

37 config: UvicornExtraConfig 

38 

39 def init_processes(self) -> None: 

40 for i in range(self.processes_num): 

41 self.config._run_extra_options["worker_id"] = i 

42 extra: dict[str, Any] = {"sockets": self.sockets} 

43 if _UVICORN_TAKES_TARGET: 

44 extra["target"] = self.target # type: ignore[attr-defined] 

45 process = Process(self.config, **extra) 

46 process.start() 

47 self.processes.append(process) 

48 

49 

50class ASGIMultiprocess: 

51 __slots__ = ( 

52 "_is_factory", 

53 "_log_level", 

54 "_run_extra_options", 

55 "_target", 

56 "_workers", 

57 ) 

58 

59 def __init__( 

60 self, 

61 target: str, 

62 args: "RunArgs", 

63 workers: int, 

64 ) -> None: 

65 self._target = target 

66 self._run_extra_options = cast_uvicorn_params(args.extra_options) 

67 self._workers = workers 

68 self._is_factory = args.is_factory 

69 self._log_level = args.log_level 

70 

71 def run(self) -> None: 

72 if not HAS_UVICORN: 

73 raise ImportError(INSTALL_UVICORN) 

74 

75 config = UvicornExtraConfig( 

76 app=self._target, 

77 factory=self._is_factory, 

78 log_level=self._log_level, 

79 workers=self._workers, 

80 **{ 

81 key: v 

82 for key, v in self._run_extra_options.items() 

83 if key in set(inspect.signature(uvicorn.Config).parameters.keys()) 

84 }, 

85 run_extra_options=self._run_extra_options, 

86 ) 

87 sock = config.bind_socket() 

88 extra: dict[str, Any] = {"sockets": [sock]} 

89 if _UVICORN_TAKES_TARGET: 

90 extra["target"] = uvicorn.Server(config).run 

91 UvicornMultiprocess(config, **extra).run()