Issue #178: modules are now cached

47 views
Skip to first unread message

Dean Hall

unread,
Jan 9, 2011, 11:32:47 AM1/9/11
to python-o...@googlegroups.com
http://code.google.com/p/python-on-a-chip/issues/detail?id=178

As a part of Issue #178, I created a dict in the builtins module that
caches modules so they aren't imported a 2nd time. Per recent
discussion, this caching is also necessary to match CPython behavior.

The good news is that I've checked it in and it works (and ipm works
this time, too).

The bad news is that the dict takes up a little bit more heap. And
it's in builtins, so it gets used immediately and applies to everybody.

For people who need to load a module and then delete references to it
to have it garbage collected, heres how:

import mymodule

# Use mymodule.
# ...
# done with mymodule

del mymodule, __md['mymodule']


I decided not to do it the sys.modules way because anybody who is low
enough of RAM to need to delete modules is probably too low on RAM to
import sys just to delete other modules.

share and enjoy,

!!Dean

Oscar Lindberg

unread,
Jan 9, 2011, 2:26:00 PM1/9/11
to python-o...@googlegroups.com
That is great. It seems to work for me.

The project that I am trying to run with pymite is about 10 different
modules, with most of them importing from some of the others. I can
now set the heap to about half what I needed before when the modules
were not cached.

What is the principle in p14p with optional features? I know that
there are a few defines that can turn off support for different things
in the vm. Would CACHE_MODULES be a candidate?

For me and my projects, this change is very welcome. But I do
understand that for programs running on platforms with very limited
memory this might not be a good thing. If it was possible, it would be
awesome if everyone could pick and choose the features they need,
without being burdened by the "unnecessary" stuff.

> --
> You are subscribed to the "python-on-a-chip"  (or p14p for short) Google
> Group.
> Site: http://groups.google.com/group/python-on-a-chip
>

Dean Hall

unread,
Jan 9, 2011, 3:30:15 PM1/9/11
to python-o...@googlegroups.com
Good question, Oscar, I never (or at least, not recently) explained
optional features.

2 years ago as the PyMite VM started growing more and more features,
its RAM footprint started growing along with those features. In order
to keep that footprint under control, I started using
HAVE_SOME_FEATURE definitions to make pieces of code compile
conditionally based on a platform's configuration as set in
pmfeatures.h (at the time) and now in pmfeatures.py. A developer
could selectively enable the features he wanted by defining the
HAVE_SOME_FEATURE or leave it disabled by not defining it.

That system work well except for the cases where there needed to be
corresponding code in the Python libraries such as __bi.py or sys.py.
I still haven't implemented a good solution for that. This is why you
still see the bytearray class commented out in src/lib/__bi.py. The
econotag platform that uses bytearrays then has a patch file to
uncomment that code in __bi.py that is applied during its build
process. Ugly, but it works.

Looking forward to r10 (The Big Rewrite), the amount of RAM that's
going to be saved by the redesign is going to allow *most* of the
HAVE_ features to be enabled. So between now and r10, I'm not going
to worry about creating new feature definitions. I'm going to let it
slide and reassess the RAM budget as I make the big changes and get
closer to the 10 release.

!!Dean

jani basha

unread,
Aug 12, 2014, 9:53:17 AM8/12/14
to python-o...@googlegroups.com
Hi,
I am working on P14P and i have taken latest release r10 from repository. I am facing issues with memory limitation while loading modules from ipm prompt. As suggested below if we have "del module_name" feature readily available , can some body please suggest me the way how to do this and in which file i need to made those changes.

As suggested by you the following snippet 

 import mymodule

        # Use mymodule.
        # ...
        # done with mymodule

        del mymodule, __md['mymodule']

in which file i need to add, its in the sample file('test.py' which i have placed in src/tools directory ) which i am loading from ipm prompt or main.py file.

Regards,
Jani Basha Shaik
Reply all
Reply to author
Forward
0 new messages