473,386 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

import keyword behaviour - performance impact if used multipletimes?

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 <dr**@gremlinhosting.com>

Jul 18 '05 #1
4 2792
> 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
Jul 18 '05 #2
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.
Jul 18 '05 #3
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?

Jul 18 '05 #4
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 | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: sandy | last post by:
Hi All, I am a newbie to MySQL and Python. At the first place, I would like to know what are the general performance issues (if any) of using MySQL with Python. By performance, I wanted to...
12
by: serge | last post by:
I have an SP that is big, huge, 700-800 lines. I am not an expert but I need to figure out every possible way that I can improve the performance speed of this SP. In the next couple of weeks I...
5
by: Ben Long | last post by:
Hi All, A partner of mine has been charged with identifying a performance / load problem currently experienced on one of our websites. I'm thinking it may be a combination of HTML formatting...
16
by: ooze | last post by:
hi all , anyone could tell me the useage of the keyworkd "extern" in c? and the difference between c & cplusplus? in the C99Rationalv5.10.pdf it lists 4 cases. while the first common cases will...
14
by: Zeng | last post by:
Would somebody know when we should seal a class? Shouldn't all classes be open up for inheritance? Thanks!
8
by: John S | last post by:
In Visual Basic, there was a keyword called "With". If you referenced an object with the keyword With you could access all properties and events just using the "." and not having to use the...
2
by: 1944USA | last post by:
I am re-architecting a C# application written as a multithreaded Windows Service and trying to squeeze every bit of performance out of it. 1) Does the thread that an object is instantiated on...
7
by: Magnus | last post by:
Im using the new binding features of Visual Studio 2005. I have done the steps to create a bound data source, and selected all 40 tables from the database. The wizard generated the necessary code...
7
by: Ron Adam | last post by:
from __future__ import absolute_import Is there a way to check if this is working? I get the same results with or without it. Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win 32 ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.