import keyword behaviour - performance impact if used multipletimes?

Discussion in 'Python' started by Andrew James, Nov 27, 2004.

  1. Andrew James

    Andrew James Guest

    Hi,
    I've been looking around on Google for the answer to this question, and
    it's beginning to really bug me. I'm making some design decisions for
    some code I'm writing, and I'm wondering whether (Good Design Decisions
    apart), there's a performance impact in importing the same module in two
    different files. For example, with the following:

    fileA.py
    -----------
    import psycopg
    class A:
    ....

    fileB.py
    import psycopg
    class B:
    a = A()
    ....

    If I run fileB, will this import the psycopg twice, or once only? Is
    this something to do with system modules being singletons?

    If someone could help me out with this it would be much appreciated.

    Regards,
    Andrew

    --
    Andrew James <>
     
    Andrew James, Nov 27, 2004
    #1
    1. Advertising

  2. Re: import keyword behaviour - performance impact if used multiple times?

    > If I run fileB, will this import the psycopg twice, or once only? Is
    > this something to do with system modules being singletons?


    It will be imported only once. Of course the statement will be read twice -
    but as this only happens while reading the *.py the first time, you needn't
    bother about performance issues.
    --
    Regards,

    Diez B. Roggisch
     
    Diez B. Roggisch, Nov 27, 2004
    #2
    1. Advertising

  3. Andrew James

    Nick Coghlan Guest

    Andrew James wrote:
    > Hi,
    > I've been looking around on Google for the answer to this question, and
    > it's beginning to really bug me. I'm making some design decisions for
    > some code I'm writing, and I'm wondering whether (Good Design Decisions
    > apart), there's a performance impact in importing the same module in two
    > different files. For example, with the following:
    >
    > fileA.py
    > -----------
    > import psycopg
    > class A:
    > ....
    >
    > fileB.py
    > import psycopg
    > class B:
    > a = A()
    > ....
    >
    > If I run fileB, will this import the psycopg twice, or once only?


    I'm guessing fileB.py should read:
    import psycopg
    import A
    ...etc

    Anyway, when Python imports modules it stores a reference to them in
    sys.modules. Any attempts to import the module again are satisfied using the
    cached version already stored in sys.modules.

    So, in your example, psycopg is imported once only, and both fileA and fileB
    would be referring to the same version of psycopg.

    > Is
    > this something to do with system modules being singletons?


    They aren't singletons in the GoF design pattern sense. However, Python's import
    machinery operates in such a way that it takes effort to get multiple version of
    the same module into memory at the same time (it *can* be done, but you have to
    work at it).

    Cheers,
    Nick.
     
    Nick Coghlan, Nov 27, 2004
    #3
  4. Andrew James

    neophyte Guest

    Re: import keyword behaviour - performance impact if used multiple times?

    Nick Coghlan wrote:
    > > Is
    > > this something to do with system modules being singletons?

    >
    > They aren't singletons in the GoF design pattern sense. However,

    Python's import
    > machinery operates in such a way that it takes effort to get multiple

    version of
    > the same module into memory at the same time (it *can* be done, but

    you have to
    > work at it).

    Given that this is exactly what I want, how can you do it?
     
    neophyte, Jan 14, 2005
    #4
  5. Andrew James

    Nick Coghlan Guest

    neophyte wrote:
    > Nick Coghlan wrote:
    >
    >> > Is
    >> > this something to do with system modules being singletons?

    >>
    >>They aren't singletons in the GoF design pattern sense. However,

    >
    > Python's import
    >
    >>machinery operates in such a way that it takes effort to get multiple

    >
    > version of
    >
    >>the same module into memory at the same time (it *can* be done, but

    >
    > you have to
    >
    >>work at it).

    >
    > Given that this is exactly what I want, how can you do it?
    >


    If you just want to reload an existing module, use the builtin "reload" function.

    Getting multiple versions of a module into sys.modules at the same time isn't
    something I've ever actually wanted to do, but the following will do it:

    Py> import sys
    Py> sys.modules["sys1"] = sys
    Py> del sys.modules["sys"]
    Py> import sys
    Py> import sys1
    Py> sys is sys1
    False

    However:
    1. Doing this at all is probably a bad idea (since you may end up duplicating
    objects that are meant to be unique within the process, and any C-extension code
    will still be shared between the multiple versions of the module)
    2. Doing it to 'sys' like I just did is an even worse idea, since you
    *definitely* end up doing 1 :)

    Cheers,
    Nick.

    --
    Nick Coghlan | | Brisbane, Australia
    ---------------------------------------------------------------
    http://boredomandlaziness.skystorm.net
     
    Nick Coghlan, Jan 15, 2005
    #5
    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. Scott Cadillac
    Replies:
    0
    Views:
    344
    Scott Cadillac
    Oct 25, 2003
  2. Aaron Anodide
    Replies:
    9
    Views:
    459
    Aaron Anodide
    Jul 30, 2003
  3. vinjvinj
    Replies:
    13
    Views:
    532
    Peter Otten
    Mar 11, 2006
  4. Rich
    Replies:
    6
    Views:
    350
    Tim Mackey
    Jan 24, 2007
  5. Replies:
    3
    Views:
    278
    Keith Thompson
    Mar 17, 2006
Loading...

Share This Page