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 start-up time by deferring slow imports. #6

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions configurator/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ class Config(ConfigNode):
"""

__slots__ = ('data', '_previous')

parsers = Parsers.from_entrypoints()
parsers = None

def __init__(self, data=None):
super(Config, self).__init__(data)
Expand Down Expand Up @@ -51,6 +50,11 @@ def from_stream(cls, stream, parser=None):
except ValueError:
pass
if not callable(parser):
# Loading the parsers takes a long time (≈200 ms) because it causes
# a lot of code to be imported (`pkg_resources`, `toml`, `yaml`,
# etc). So don't load the parsers until just before we need them.
if cls.parsers is None:
cls.parsers = Parsers.from_entrypoints()
parser = cls.parsers.get(parser)
return cls(parser(stream))

Expand Down
5 changes: 3 additions & 2 deletions configurator/parsers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from pkg_resources import iter_entry_points


class ParseError(Exception):
"""
Expand All @@ -12,6 +10,9 @@ class Parsers(dict):

@classmethod
def from_entrypoints(cls):
# `pkg_resources` is slow to import, so defer until we need it.
from pkg_resources import iter_entry_points
Copy link
Member

Choose a reason for hiding this comment

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

This is a bit ugly. Why is important pkg_resources so slow? Have you reported the issue upstream at https://github.com/pypa/setuptools?

Copy link
Author

Choose a reason for hiding this comment

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

It is a bit ugly. I don't know why it's so slow, but there are already a couple issues open relating to this: pypa/setuptools#510 pypa/setuptools#926.


parsers = cls()
for entrypoint in iter_entry_points(group='configurator.parser'):
try:
Expand Down