Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > import keyword behaviour - performance impact if used multipletimes?

Reply
Thread Tools

import keyword behaviour - performance impact if used multipletimes?

 
 
Andrew James
Guest
Posts: n/a
 
      11-27-2004
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 <>

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      11-27-2004
> 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
 
Reply With Quote
 
 
 
 
Nick Coghlan
Guest
Posts: n/a
 
      11-27-2004
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.
 
Reply With Quote
 
neophyte
Guest
Posts: n/a
 
      01-14-2005
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?

 
Reply With Quote
 
Nick Coghlan
Guest
Posts: n/a
 
      01-15-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance Impact of ASP.NET 1.1 and 2.0 Side by Side Rich ASP .Net 6 01-24-2007 10:54 PM
Multithreaded application performance impact of global arrays groups@barnholt.net C Programming 3 03-17-2006 06:51 PM
Performance impact of using decorators vinjvinj Python 13 03-11-2006 08:39 AM
Can Novell Security impact ASP.NET performance? Scott Cadillac ASP .Net 0 10-25-2003 04:40 PM
runtime performance impact of template usage Aaron Anodide C++ 9 07-30-2003 06:45 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57