pythonic way to optimize access to imported value?
holger krekel
pyth at devel.trillke.net
Mon Nov 18 08:20:09 EST 2002
Jeff Epler wrote:
> On Mon, Nov 18, 2002 at 04:03:30AM +0000, Bengt Richter wrote:
> > IOW, IMO there ought to be a way to do a temporary global-side-effect-free
> > import and delete all traces of its having been done -- without restarting
> > the interpreter or invoking a second interpreter instance.
>
> Let us know when you've implemented it.
yes bengt, go for it :-)
> Some of the problems:
>
> Modules need not be side-effect-free. For instance, the following is a
> (very harmful) legal Python module:
> from os import system
> system("rm -rf /")
sure, removing modules can only be as side effect free as the modules
itself is. You can't even reverse print 'hello world'.
I am sure there are uses for removing module objects. I'd recommend
going for an 'try_remove_module' function which tries to achieve
module unloading. It's probably neccessary to assert that there
are no references to any objects in that to-be-removed module.
So you might be able to say:
import somemodule
# do stuff with somemodule but don't permanently bind to any
# of its objects
try_remove_module(somemodule)
del somemodule # or do a hack inside try_remove_module
# somemodule should now be removed from memory, unless it's
# a C-module or other special cases to be determined :-)
On a side note, yesterday i talked to a perl hacker and he said
that mod_perl suffers a lot from interpreter bloat because it's
very hard to unload something plus perl has memory allocation
problems with its mutable strings (memory usage always stays at
the maximum size that string ever had). python doesn't have the latter
problem because its strings are immutable. Don't know about
how lists and dicts are handled, though.
So I think that e.g. apps with mod_python could benefit from
beeing able to unload some modules which are only temporarily used
as helpers.
regards,
holger
More information about the Python-list
mailing list