Unloading a module

Discussion in 'Python' started by lallous, Oct 22, 2009.

  1. lallous

    lallous Guest

    Hello Group,

    If a reference to an imported module reaches zero will Python cleanup
    everything related to that module and unload the compiled code, etc, etc...?

    For example:

    import sys
    m = [__import__(str(x)) for x in xrange(1,4)]
    del sys.modules['1']
    del m[0]
    print m

    Is module['1'] really unloaded or just it is not reachable but still loaded?

    Thanks,
    Elias
     
    lallous, Oct 22, 2009
    #1
    1. Advertising

  2. En Thu, 22 Oct 2009 11:59:58 -0300, lallous <> escribió:

    > If a reference to an imported module reaches zero will Python cleanup
    > everything related to that module and unload the compiled code, etc,
    > etc...?


    The module object itself (as any other object whose reference count
    reaches zero) will be destroyed. This in turn will decrement the reference
    count of its namespace (its __dict__), and when that reaches zero it will
    decrement the reference count of any object referenced (classes defined in
    the module, global variables...). If there are no other references to
    them, they will be destroyed too, as with any other object. It's always
    the same story.

    > For example:
    >
    > import sys
    > m = [__import__(str(x)) for x in xrange(1,4)]
    > del sys.modules['1']
    > del m[0]
    > print m
    >
    > Is module['1'] really unloaded or just it is not reachable but still
    > loaded?


    Try with sys.getrefcount(the_module); when it reaches 2 you know
    the_module is the last reference to the object [remember that getrefcount
    always returns one more than the actual reference count]
    In your example above, after the __import__ line, there are two references
    to the module '1': one in sys.modules, and one in the m list. Once you
    remove them (with del, as in your example), the module object itself is
    destroyed, yes.

    --
    Gabriel Genellina
     
    Gabriel Genellina, Oct 23, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. greg

    leaks unloading AppDomain

    greg, Jun 12, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,038
    Lionel LASKE
    Jun 13, 2005
  2. alien2_51
    Replies:
    0
    Views:
    552
    alien2_51
    Jun 27, 2003
  3. Guru Prasad

    unloading assemblies - delayed ?

    Guru Prasad, Jul 16, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    424
    Guru Prasad
    Jul 21, 2003
  4. Ben Miller [msft]
    Replies:
    0
    Views:
    605
    Ben Miller [msft]
    Aug 5, 2003
  5. lordofcode
    Replies:
    15
    Views:
    1,596
Loading...

Share This Page