-
Notifications
You must be signed in to change notification settings - Fork 227
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
Avoid the limitation of not being able to import rclpy C extensions at module level [alternative] #420
Conversation
…t module level Signed-off-by: ivanpauno <ivanpauno@ekumenlabs.com>
CI up to rclpy (on windows, also built up to launch_testing_ros)
|
So, this PR is solving all the test failures on Windows CI, plus allowing to import the C extensions at module level. |
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.
As far as I understand this patch the C extension would immediately be loaded when import rclpy
is invoked. I thought the general goal was to delay that until any function from the C extension is actually being used?
@@ -67,7 +67,7 @@ function(configure_python_c_extension_library _library_name) | |||
) | |||
|
|||
install(TARGETS ${_library_name} | |||
DESTINATION "${PYTHON_INSTALL_DIR}/${PROJECT_NAME}" | |||
DESTINATION "${PYTHON_INSTALL_DIR}/${PROJECT_NAME}_impl" |
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.
This package doesn't own the namespace rclpy_impl
. If another package with that name installs its files they will collide with this one.
Hmm, why is that a goal? |
As the comments on the local imports state: "to avoid loading extensions on module import". First of all if you import any Python module it doesn't need to load the C extensions yet - hence it is faster and lighter weight. Second it avoid the race condition between any customization you might want to do before trying to load the C extensions. Third any tool which needs to import the Python modules but doesn't want to ever call any of the API can do so without the need to have / load the C extensions. |
Is there an observable difference in speed? Do we have a measure of how bigger the memory footprint is? Correct me if I'm wrong but I'd expect
What kind of customization? Are we doing so? Could a downstream package achieve the same effect by delaying
Which goes back to the first statement, unless we apply that same logic to every single module import. I'd like to further understand why we need to give special treatment for an otherwise regular CPython module. It certainly makes it non-standard and creates issues from time to time -- which are easy to fix, sure, but only once you've understood all the implications of how we build and install |
@hidmic @dirk-thomas I've worked in a lazy import alternative, but I first want to finish with this discussion. I understand @hidmic point, I think we're doing kind of a "premature optimization". There is currently two problems:
|
The primary goal for doing lazy load is not performance improvement. It is to avoid the current race and in use cases which don't need the C extension not requiring it to be loaded.
I think you are missing some use cases:
With my proposal to convert the globally initialized handles into functions providing a lazily loaded singleton instance of the C extension that would not be the case anymore and each library would be loaded only when needed and on demand.
See my above comment why I don't think that is feasible. Also the current approach would work just fine as is. In the cases it currently fails due to the race it would work once we do lazy loading. |
Closing, because this is outdated. |
@ivanpauno Is there any open ticket for this? If not, please create one and reference the previous approaches. Also is the branch still needed? |
See #560.
Deleted. |
Alternative to #417.
In this case, instead of installing the C extensions as a module of
rclpy
package, I installed them in a new packagerclpy_impl
.In this way, we don't need to "hack" the imports while testing.
This solve the same problem as #417, and also, it should solve the testing problems of
rclpy
on windows whenlaunch_testing_ros
is present.