Nick Coghlan <ncogh...@gmail.com> added the comment:

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.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33331>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to