[Python-Dev] Python startup optimization: script vs. service (original) (raw)
Brett Cannon brett at python.org
Mon Oct 2 13:29:50 EDT 2017
- Previous message (by thread): [Python-Dev] Python startup optimization: script vs. service
- Next message (by thread): [Python-Dev] Python startup optimization: script vs. service
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Mon, 2 Oct 2017 at 08:00 Christian Heimes <christian at python.org> wrote:
On 2017-10-02 15:26, Victor Stinner wrote: > 2017-10-02 13:10 GMT+02:00 INADA Naoki <songofacandy at gmail.com>: >> https://github.com/python/cpython/pull/3796 >> In this PR, lazy loading only happens when uuid1 is used. >> But uuid1 is very uncommon for nowdays. > > Antoine Pitrou added a new C extension uuid which is imported as soon > as uuid(.py) is imported. On Linux at least, the main "overhead" is > still done on "import uuid". But Antoine change optimized a lot > "import uuid" import time! > >> https://github.com/python/cpython/pull/3757 >> In this PR, singledispatch is lazy loading types and weakref. >> But singledispatch is used as decorator. >> So if web application uses singledispatch, it's loaded before preforking. > > While "import module" is fast, maybe we should use sometimes a global > variable to cache the import. > > module = None > def func(): > global module > if module is None: import module > ... > > I'm not sure that it's possible to write an helper for such pattern.
I would rather like to see a function in importlib that handles deferred imports: modulename = importlib.deferredimport('modulename') def deferredimport(name): if name in sys.modules: # special case 'None' here return sys.modules[name] else: return ModuleProxy(name) ModuleProxy is a module type subclass that loads the module on demand.
My current design for an opt-in lazy importing setup includes an explicit function for importlib that's mainly targeted for the stdlib and it's startup module needs, but could be used by others: https://notebooks.azure.com/Brett/libraries/di2Btqj7zSI/html/Lazy%20importing.ipynb . -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20171002/76bd50f3/attachment.html>
- Previous message (by thread): [Python-Dev] Python startup optimization: script vs. service
- Next message (by thread): [Python-Dev] Python startup optimization: script vs. service
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]