Lazy loading of python modules
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
Lazy loading of python modules
While investigating the slow loading of the path workbench I found that one of the main culprits was imports of other workbenches. I was able to fix it in a few places by using deferred loading, but some modules were used in several places, and deferred importing would be awkward. I found the LazyLoader class from tensorflow, described here https://wil.yegelwel.com/lazily-importi ... n-modules/ source is here https://github.com/tensorflow/tensorflo ... _loader.py . It seems to work well from testing, and could be used by other workbenches. It would be a single file copied, so no external deps, it's apache licensed which is compatible with LGPL2+ (specifically LGPL3). Where should I put it to be usable by all workbenches? App, Base?
Re: Lazy loading of python modules
Probably the best place is src/3rdParty. This should be done for all external packages that are copied to our source tree, although I think this is not followed strictly, as some workbenches include third party code inside its own src/Mod/ directory.
About lazy loading, a trick that we do in the Draft and Arch workbenches is basically load the required module at runtime, when we run a specific function.
Code: Select all
def some_operation():
import Part
do_something(Part.Face())
...
However, if you can find a more natural and Pythonic solution, that would be good. I haven't tried this LazyLoader class before, so it's great if you can investigate more about it.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
Re: Lazy loading of python modules
I moved several imports into the function body of modules. I was looking in to using lazyloader instead because some modules have several functions that need the import statements. It tends to clutter things up. Also with lazyloader even if there's just one import, we can still put it at the top, and it won't be loaded until the first use. I've done a quick test and it seems to work well.
Re: Lazy loading of python modules
What types of imports? From Path itself or from other workbenches. I don't know how exactly the workbench is setup, but last time I took a look at it, it had plenty of modules, and classes upon classes.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
Re: Lazy loading of python modules
In Path this could indeed be beneficial. I would guess that the external libraries take quite some time. And if 3DSurface is not used in the model, opencamlib does not have to be loaded. However, this may require some more changes, because currently 3DSurface is enabled only if the library can be accessed.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
Re: Lazy loading of python modules
Path Job Gui import Draft and DraftVecUtils, PathJob and several other modules import ArchPanel, etc. A lot of the modules only used the imported workbenches for a single function, so I moved those down in to the function body for testing, but it wasn't always the case. Also I'd like to keep the imports at the top of the file, mainly for readability.
When I get a chance I'll throw together a PR for this. Just wanted some input first on where to put the LazyLoader class, and the license compatibility. Unless anyone has other input I'll throw it in third party for now.
Re: Lazy loading of python modules
Can you post the repo that you are conducting these tests in so we can see the code before you submit a PR ?
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
Re: Lazy loading of python modules
This will be needed at runtime?
If so we will need to copy the class to the build dir and add it to the INSTALL project.
"fight the good fight"
Re: Lazy loading of python modules
I'm working on it here https://github.com/etrombly/FreeCAD/tree/lazyloader . I moved it out of path and into 3rd Party, so it's not working currently. Trying to figure out the cmake file for it. I think I have it copying to the build dir and installing correctly, but can't get it to add to the python path. Just learning cmake now.
Re: Lazy loading of python modules
I think you can install it to the Ext/ directory. It's supposed to be in the Python path.
Alternatively, you may wish to add it to Mod/ as this should also be in the path, but that doesn't look as nice.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.