Skip to content

Commit

Permalink
Lazy load Reload and Multiprocess supervisors
Browse files Browse the repository at this point in the history
  • Loading branch information
Mickaël Guérin committed Jan 14, 2023
1 parent e35b34f commit 79becec
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
2 changes: 1 addition & 1 deletion tests/supervisors/test_multiprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import TYPE_CHECKING, List, Optional

from uvicorn import Config
from uvicorn.supervisors import Multiprocess
from uvicorn.supervisors.multiprocess import Multiprocess

if TYPE_CHECKING:
from asgiref.typing import ASGIReceiveCallable, ASGISendCallable, Scope
Expand Down
5 changes: 3 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from uvicorn.config import Config
from uvicorn.main import main as cli
from uvicorn.server import Server
from uvicorn.supervisors import ChangeReload, Multiprocess
from uvicorn.supervisors.multiprocess import Multiprocess
from uvicorn.supervisors.watchfilesreload import WatchFilesReload

HEADERS = "Content-Security-Policy:default-src 'self'; script-src https://example.com"
main = importlib.import_module("uvicorn.main")
Expand Down Expand Up @@ -71,7 +72,7 @@ def test_cli_call_change_reload_run() -> None:
runner = CliRunner()

with mock.patch.object(Config, "bind_socket") as mock_bind_socket:
with mock.patch.object(ChangeReload, "run") as mock_run:
with mock.patch.object(WatchFilesReload, "run") as mock_run:
result = runner.invoke(cli, ["tests.test_cli:App", "--reload"])

assert result.exit_code == 0
Expand Down
8 changes: 6 additions & 2 deletions uvicorn/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
WSProtocolType,
)
from uvicorn.server import Server, ServerState # noqa: F401 # Used to be defined here.
from uvicorn.supervisors import ChangeReload, Multiprocess

if typing.TYPE_CHECKING:
from asgiref.typing import ASGIApplication
Expand Down Expand Up @@ -560,9 +559,14 @@ def run(
sys.exit(1)

if config.should_reload:
from uvicorn.supervisors import get_reload_class

sock = config.bind_socket()
ChangeReload(config, target=server.run, sockets=[sock]).run()
reload_class = get_reload_class()
reload_class(config, target=server.run, sockets=[sock]).run()
elif config.workers > 1:
from uvicorn.supervisors.multiprocess import Multiprocess

sock = config.bind_socket()
Multiprocess(config, target=server.run, sockets=[sock]).run()
else:
Expand Down
28 changes: 15 additions & 13 deletions uvicorn/supervisors/__init__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
from typing import TYPE_CHECKING, Type

from uvicorn.supervisors.basereload import BaseReload
from uvicorn.supervisors.multiprocess import Multiprocess

if TYPE_CHECKING:
ChangeReload: Type[BaseReload]
else:
from uvicorn.supervisors.basereload import BaseReload


def get_reload_class() -> "Type[BaseReload]":
try:
from uvicorn.supervisors.watchfilesreload import (
WatchFilesReload as ChangeReload,
)
from uvicorn.supervisors.watchfilesreload import WatchFilesReload

return WatchFilesReload
except ImportError: # pragma: no cover
try:
from uvicorn.supervisors.watchgodreload import (
WatchGodReload as ChangeReload,
)
from uvicorn.supervisors.watchgodreload import WatchGodReload

return WatchGodReload
except ImportError:
from uvicorn.supervisors.statreload import StatReload as ChangeReload
from uvicorn.supervisors.statreload import StatReload

return StatReload


__all__ = ["Multiprocess", "ChangeReload"]
__all__ = ["get_reload_class"]

0 comments on commit 79becec

Please sign in to comment.