Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve import time #1846

Merged
merged 2 commits into from Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I don't recall this wrong, I believe that when this was implemented None was the default, but it was changed to the default value that we currently have. I can be wrong tho...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value was None before this PR. IMO exposing the default value was just a side effect of fixing the breaking change in h11.

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