Skip to content

Commit

Permalink
Improve import time (#1846)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgu committed Feb 7, 2023
1 parent 19ccd8a commit 82367be
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
16 changes: 5 additions & 11 deletions uvicorn/config.py
Expand Up @@ -21,8 +21,6 @@
Union,
)

from h11._connection import DEFAULT_MAX_INCOMPLETE_EVENT_SIZE

from uvicorn.logging import TRACE_LOG_LEVEL

if sys.version_info < (3, 8): # pragma: py-gte-38
Expand All @@ -32,14 +30,6 @@

import click

try:
import yaml
except ImportError: # pragma: no cover
# If the code below that depends on yaml is exercised, it will raise a NameError.
# Install the PyYAML package or the uvicorn[standard] optional dependencies to
# enable this functionality.
pass

from uvicorn.importer import ImportFromStringError, import_from_string
from uvicorn.middleware.asgi2 import ASGI2Middleware
from uvicorn.middleware.message_logger import MessageLoggerMiddleware
Expand Down Expand Up @@ -250,7 +240,7 @@ def __init__(
ssl_ciphers: str = "TLSv1",
headers: Optional[List[Tuple[str, str]]] = None,
factory: bool = False,
h11_max_incomplete_event_size: int = DEFAULT_MAX_INCOMPLETE_EVENT_SIZE,
h11_max_incomplete_event_size: Optional[int] = None,
):
self.app = app
self.host = host
Expand Down Expand Up @@ -410,6 +400,10 @@ def configure_logging(self) -> None:
loaded_config = json.load(file)
logging.config.dictConfig(loaded_config)
elif self.log_config.endswith((".yaml", ".yml")):
# Install the PyYAML package or the uvicorn[standard] optional
# dependencies to enable this functionality.
import yaml

with open(self.log_config) as file:
loaded_config = yaml.safe_load(file)
logging.config.dictConfig(loaded_config)
Expand Down
7 changes: 3 additions & 4 deletions uvicorn/main.py
Expand Up @@ -7,7 +7,6 @@
import typing

import click
from h11._connection import DEFAULT_MAX_INCOMPLETE_EVENT_SIZE

import uvicorn
from uvicorn.config import (
Expand Down Expand Up @@ -344,7 +343,7 @@ def print_version(ctx: click.Context, param: click.Parameter, value: bool) -> No
"--h11-max-incomplete-event-size",
"h11_max_incomplete_event_size",
type=int,
default=DEFAULT_MAX_INCOMPLETE_EVENT_SIZE,
default=None,
help="For h11, the maximum number of bytes to buffer of an incomplete event.",
)
@click.option(
Expand Down Expand Up @@ -398,7 +397,7 @@ def main(
headers: typing.List[str],
use_colors: bool,
app_dir: str,
h11_max_incomplete_event_size: int,
h11_max_incomplete_event_size: typing.Optional[int],
factory: bool,
) -> None:
run(
Expand Down Expand Up @@ -498,7 +497,7 @@ def run(
use_colors: typing.Optional[bool] = None,
app_dir: typing.Optional[str] = None,
factory: bool = False,
h11_max_incomplete_event_size: int = DEFAULT_MAX_INCOMPLETE_EVENT_SIZE,
h11_max_incomplete_event_size: typing.Optional[int] = None,
) -> None:
if app_dir is not None:
sys.path.insert(0, app_dir)
Expand Down
8 changes: 7 additions & 1 deletion uvicorn/protocols/http/h11_impl.py
Expand Up @@ -6,6 +6,7 @@
from urllib.parse import unquote

import h11
from h11._connection import DEFAULT_MAX_INCOMPLETE_EVENT_SIZE

from uvicorn.config import Config
from uvicorn.logging import TRACE_LOG_LEVEL
Expand Down Expand Up @@ -79,7 +80,12 @@ def __init__(
self.logger = logging.getLogger("uvicorn.error")
self.access_logger = logging.getLogger("uvicorn.access")
self.access_log = self.access_logger.hasHandlers()
self.conn = h11.Connection(h11.SERVER, config.h11_max_incomplete_event_size)
self.conn = h11.Connection(
h11.SERVER,
config.h11_max_incomplete_event_size
if config.h11_max_incomplete_event_size is not None
else DEFAULT_MAX_INCOMPLETE_EVENT_SIZE,
)
self.ws_protocol_class = config.ws_protocol_class
self.root_path = config.root_path
self.limit_concurrency = config.limit_concurrency
Expand Down

0 comments on commit 82367be

Please sign in to comment.