-
Notifications
You must be signed in to change notification settings - Fork 639
Speed up imports by removing pkg_resources #1110
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
Conversation
This is used for loading external interfaces as plugins (see https://python-can.readthedocs.io/en/develop/interfaces.html) so it's not possible to just remove it. You could however use importlib.metadata instead if possible, and fallback to setuptools on older Python versions. |
Okay, this was the response I was hoping to hear. I'll try the suggested changes and commit some additional changes |
I think the last solution to this problem is making some architectural changes to how bus objects are initialized. A suggestion was made to initialize plugins on-demand, in order to reduce import time for other users. |
Just revert bus.py since it is related to another PR. Then I think it looks good. |
…imports # Conflicts: # can/interfaces/__init__.py
This reverts commit 43c0c55
Made the mistake of rebasing and had to revert my changes for canbus load calculations, but things should be fine. I made some additional changes to allow older versions than Python 3.8 to only load user-installed interfaces on-demand. I really would like these features backported as well in order to improve import time for older versions of Python. But this brings up an issue where I can no longer have |
It's probably a limited number of users that rely on VALID_INTERFACES but since it is a breaking change I'm hesitant to include that. If one wants to take advantage of all optimizations they should update Python and other packages to the latest version. Python 3.9 will soon be the most commonly used version as it gets included in various distributions. |
I think that's a reasonable issue. I'll go ahead and revert the last commit. |
…tlib is not installed" This reverts commit 2fed532
Typically the imports would stay at the top of the module and use a feature flag (like |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, I wasn't aware of importlib's entry_points
api.
Codecov Report
@@ Coverage Diff @@
## develop #1110 +/- ##
===========================================
+ Coverage 70.49% 70.50% +0.01%
===========================================
Files 79 79
Lines 7666 7672 +6
===========================================
+ Hits 5404 5409 +5
- Misses 2262 2263 +1 |
I created this PR to address issue #935, since I was having similar issues with imports. This PR is built off my previous PR #1105 since I think this change warrants more discussion.
Originally, python-can took four seconds to import. After removing the call to run
iter_entry_points
, the import time was down to three seconds. Removing the import call forpkg_resources
reduced the import time by two seconds down to only needing one second for importing python-can.I manually ran
iter_entry_points
in a similar setup as when the import process is run, to see if the dictionary forBACKENDS
would change, but it seems nothing would change at all. Since everything is manually defined, it seems there's no need foriter_entry points
?Am I missing something here? I tested these changes with
socketcan
and I didn't have any issues.