This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ncoghlan
Recipients barry, brett.cannon, eric.snow, ncoghlan, pitrou, serhiy.storchaka
Date 2018-06-09.04:38:42
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1528519122.74.0.592728768989.issue33331@psf.upfronthosting.co.za>
In-reply-to
Content
It also occurred to me to ask whether or not moving modules to the end of sys.modules each time they're successfully imported would help solve the problem (albeit at the expense of making import cache hits more expensive).

I don't think it does, though, since in my example above, the least-recently-imported ordering would end up looking like:

    __main__, A, D, X, Y, B, Z, C

Since D was only imported by C, and hence never gets moved to the end later, even when C gets moved by the import from Z.

Instead, if we truly wanted to solve the problem comprehensively, we'd need to:

1. Use a context variable to track a stack of "currently running imports"
2. Track (as a process global) a directed (potentially cyclic!) graph of which modules imported other modules (i.e. the import of the target module started while the importing module was the currently active import on the stack). Lazy imports would typically show up as being initiated by __main__.
3. During shutdown, linearise the shutdown order for any modules which weren't already cleaned up by the cyclic GC.

Taking better advantage of the existing cyclic GC seems like it should be simpler, though, and would hopefully be enough to handle all but the most complex edge cases.
History
Date User Action Args
2018-06-09 04:38:42ncoghlansetrecipients: + ncoghlan, barry, brett.cannon, pitrou, eric.snow, serhiy.storchaka
2018-06-09 04:38:42ncoghlansetmessageid: <1528519122.74.0.592728768989.issue33331@psf.upfronthosting.co.za>
2018-06-09 04:38:42ncoghlanlinkissue33331 messages
2018-06-09 04:38:42ncoghlancreate