473,394 Members | 2,048 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,394 software developers and data experts.

module import performance question

I have a cgi script that imports modules based on a user config file.
I am suffering some performance problems when I import these modules.
Some logging revealed that it seems to be taking about 1.3 seconds to
import these modules. This is running on a Windows 2000 box with a
Pentium II 400 processor, Python 2.1, Apache 1.3.19 as the web server.
I don't need screaming performance numbers, but this time is
excessive. Is it me, or does that number seem kind of slow? Any code
optimizations or recommendations? I would like to keep the
architecture the same, just standard cgi scripts, so mod_python looks
like it's a bit more involved than I would like.

Here is a code snip:

dyn_blocks = {} # set up a dict of the dynamic imports
if confok: # was the configuration present?
blocks = getBlocks(config) # get the application info from config
for block in blocks: # each block indictates the ap & pg (module)
ap = string.split(string.strip(block[1:len(block)]))[0]
pg = string.split(string.strip(block[1:len(block)]))[1]
sys.path.append(getAppPath(ap)) # add module dir to sys.path
if (block[0] == '~'):
try:
dyn_blocks[ap + "_" + pg] = __import__(pg)
except:
Log.stacktrace(Log.ERROR) # log problem importing module

Any help is appreciated.
Jul 18 '05 #1
4 1902

"Jeff Sykes" <je**@cowz.com> wrote in message
news:72**************************@posting.google.c om...
I have a cgi script that imports modules based on a user config file. I am suffering some performance problems when I import these modules. Some logging revealed that it seems to be taking about 1.3 seconds to import these modules. This is running on a Windows 2000 box with a
Pentium II 400 processor, Python 2.1, Apache 1.3.19 as the web server. I don't need screaming performance numbers, but this time is
excessive. Is it me, or does that number seem kind of slow?
How many modules are being imported?
Any code
optimizations or recommendations?


Don't append to sys.path. Try sys.path.insert(0,addlpath). Then the
import will get an immediate hit and always the right module rather
than stepping through all the preceeding directories.

HTH,
Emile van Sebille
em***@fenx.com
Jul 18 '05 #2
Thanks, Emile, that's a good idea. I found another issue, too. See my
response to another poster below:
I think there was a different culprit, though. After some more
dilligent logging, I realized that immediately after import I
called upon an attribute of the imported module that wouldn't
_necessarily_ be present. When the attribute was not present,
it raised an exception, which I caught. This exception handling
was a real performance anchor! I now check hasattr() before I
execute this block of code, and this has helped performance
significantly.

Here's a snip of the block that occured right after import:

try:
# next line was not there before
if hasattr(dyn_blocks[key], "BUFFER_ON"):
context.setBufferFlag(dyn_blocks[key].BUFFER_ON)
except:
Log.stacktrace(Log.ERROR)

I guess the lesson I learned is don't use exception handling
unnecessarily. Which I know not to do anyway. Sigh. Back to
school for me.

"Emile van Sebille" <em***@fenx.com> wrote in message news:<bo*************@ID-11957.news.uni-berlin.de>... "Jeff Sykes" <je**@cowz.com> wrote in message
news:72**************************@posting.google.c om...
I have a cgi script that imports modules based on a user config

file.
I am suffering some performance problems when I import these

modules.
Some logging revealed that it seems to be taking about 1.3 seconds

to
import these modules. This is running on a Windows 2000 box with a
Pentium II 400 processor, Python 2.1, Apache 1.3.19 as the web

server.
I don't need screaming performance numbers, but this time is
excessive. Is it me, or does that number seem kind of slow?


How many modules are being imported?
Any code
optimizations or recommendations?


Don't append to sys.path. Try sys.path.insert(0,addlpath). Then the
import will get an immediate hit and always the right module rather
than stepping through all the preceeding directories.

HTH,
Emile van Sebille
em***@fenx.com

Jul 18 '05 #3
je**@cowz.com (Jeff Sykes) wrote in message news:<72**************************@posting.google. com>...
....
....
....
Here is a code snip:

dyn_blocks = {} # set up a dict of the dynamic imports
if confok: # was the configuration present?
blocks = getBlocks(config) # get the application info from config
for block in blocks: # each block indictates the ap & pg (module)
doen't know how long is len(blocks), if it's very long you could
accelerate the *for loop* be replacing the following two lines with:

(ap,pg) = string.split(string.strip(block[1:]))
**Remark: block[1:len(block)] is the same as block[1:], but without
calulating len(block) everytime, further more the split
and the strip is done only once
ap = string.split(string.strip(block[1:len(block)]))[0]
pg = string.split(string.strip(block[1:len(block)]))[1]


....
....
....

Regards
Peter
Jul 18 '05 #4
je**@cowz.com (Jeff Sykes) wrote in message news:<72**************************@posting.google. com>...
...
Here's a snip of the block that occured right after import:

try:
# next line was not there before
if hasattr(dyn_blocks[key], "BUFFER_ON"):
context.setBufferFlag(dyn_blocks[key].BUFFER_ON)
except:
Log.stacktrace(Log.ERROR)
I guess the lesson I learned is don't use exception handling
unnecessarily. Which I know not to do anyway. Sigh. Back to
school for me.
Wrong lesson. Python wants you to use exception handling.
The time sink above code is the unnecessary uses of: Log.stacktrace(Log.ERROR)


A couple of Alternatives:
## smaller, not quite careful enough for my taste
try:
context.setBufferFlag(dyn_blocks[key].BUFFER_ON)
except AttributeError:
pass
except:
Log.stacktrace(Log.ERROR)

## better, more to my taste
try:
try:
flag = dyn_blocks[key].BUFFER_ON
except AttributeError:
pass
else:
context.setBufferFlag(flag)
except:
Log.stacktrace(Log.ERROR)

-Scott
Jul 18 '05 #5

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

Similar topics

9
by: Paul Rubin | last post by:
That's what the Python style guides advise. They don't seem to like def frob(x): import re if re.search('sdfxyz', x): ... instead preferring that you pollute your module's global namespace...
4
by: Brad Tilley | last post by:
When memory usage is a concern, is it better to do: from X import Y or import X Also, is there a way to load and unload modules as they are needed. I have some scripts that sleep for...
6
by: Burton Samograd | last post by:
Hi, I'm writing an app that stores some user configuration variables in a file ~/.program/config, which it then imports like so: import sys from posix import environ...
9
by: Phoe6 | last post by:
Hi, Following are my files. In the format: Filename ---- content ---- config1.txt
10
by: Ben Finney | last post by:
Howdy all, Question: I have Python modules named without '.py' as the extension, and I'd like to be able to import them. How can I do that? Background: On Unix, I write programs intended to...
12
by: reubendb | last post by:
Hello, I am new to Python. I have the following question / problem. I have a visualization software with command-line interface (CLI), which essentially is a Python (v. 2.5) interpreter with...
0
by: Fredrik Lundh | last post by:
Jeff Dyke wrote: so how did that processing use the "mymodulename" name? the calling method has nothing to do with what's considered to be a local variable in the method being called, so...
0
by: norseman | last post by:
mercado mercado wrote: =================================== I started to import a module using its path and now see what you mean. Python is missing the concept: Programmer dictates what machine...
2
by: Robert Dailey | last post by:
Hi, I'm currently using boost::python::import() to import Python modules, so I'm not sure exactly which Python API function it is calling to import these files. I posted to the Boost.Python...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.