473,383 Members | 1,822 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,383 software developers and data experts.

Import in a separate thread

I want to import a long list of modules in a separate thread to speed
things up. How can I make the modules imported in that separate thread
accessible outside the method?

===========================
import os

# import rest in a separate thread
def importRest():
import audio
import socket
thread.start_new_thread(importRest,())

# audio.somemethod() would fail here
===========================

Feb 25 '06 #1
7 1844
"cyberco" <cy*****@gmail.com> writes:
I want to import a long list of modules in a separate thread to speed
things up. How can I make the modules imported in that separate thread
accessible outside the method?
It won't speed things up unless the main thread is waiting for user
input during those imports, or something like that. Threads in Python
don't really run in parallel.
# import rest in a separate thread
def importRest():
import audio
import socket
thread.start_new_thread(importRest,())

# audio.somemethod() would fail here


Here's one way:

def importRest():
global audio
import audio as local_audio
audio = local_audio
# etc.

There's surely other ways that are better.
Feb 25 '06 #2
cyberco wrote:
I want to import a long list of modules in a separate thread to speed
things up. How can I make the modules imported in that separate thread
accessible outside the method?


Sounds like premature optimization. Speed "things" up? What things?
How long is it taking now to load the modules you are loading? Even the
wxPython demo takes only a few seconds to load on a decent machine, and
that's loading a *heck* of a lot of stuff. (And it takes the
conventional approach to this issue too, which is to display a splash
screen while things load.)

-Peter

Feb 25 '06 #3
Peter Hansen <pe***@engcorp.com> writes:
Sounds like premature optimization. Speed "things" up? What things?
How long is it taking now to load the modules you are loading? Even
the wxPython demo takes only a few seconds to load on a decent
machine, and that's loading a *heck* of a lot of stuff.


I know that whenever I start IDLE and it's not already cached, it
takes a significant amount of time, and that doesn't even use
wxPython.
Feb 25 '06 #4
Paul Rubin wrote:
Peter Hansen <pe***@engcorp.com> writes:
Sounds like premature optimization. Speed "things" up? What things?
How long is it taking now to load the modules you are loading? Even
the wxPython demo takes only a few seconds to load on a decent
machine, and that's loading a *heck* of a lot of stuff.


I know that whenever I start IDLE and it's not already cached, it
takes a significant amount of time, and that doesn't even use
wxPython.


I don't dispute that there are in the world programs that take too long
to start by even objective measures. I don't dispute that you, Paul,
might consider IDLE to take too long to start. I don't think I was
responding to you, however, but to the OP, who hasn't described anything
about his situation except that he believes importing modules in a
separate thread might "speed things up". To me, that sounded like the
words of someone who not only might not be aware of the excellent advice
regarding premature optimization, but who might also not suffer from a
significant startup delay but who simply imagined that perhaps using a
"trick" to import things differently might make something run faster.
Understanding why he wants to do this is important to knowing what the
best approach is, whether it's to say "don't do it" or to point to
alternatives (such as a splash screen) which others have found to solve
the problem.

Just because it appears some people prematurely cry "premature
optimization" doesn't mean that it's an invalid thing to ask a poster to
consider.

-Peter

Feb 25 '06 #5
Well, it is for the python implementation for Nokia Series 60 phones,
and loading lots of modules in such constrained environments can
certainly slow things down. The splashscreen idea is what I want to do,
but that requires the loading to continue in a background thread.

Feb 25 '06 #6
On 2006-02-25, cyberco <cy*****@gmail.com> wrote:
Well, it is for the python implementation for Nokia Series 60 phones,
and loading lots of modules in such constrained environments can
certainly slow things down. The splashscreen idea is what I want to do,
but that requires the loading to continue in a background thread.


Ah, so you're _not_ trying to speed things up. You just want
to re-arrange the order in which things happen. While threads
rarely help the former, they do work well for the latter.

--
Grant Edwards grante Yow! You should all JUMP
at UP AND DOWN for TWO HOURS
visi.com while I decide on a NEW
CAREER!!
Feb 25 '06 #7
cyberco wrote:
I want to import a long list of modules in a separate thread to speed
things up. How can I make the modules imported in that separate thread
accessible outside the method?

===========================
import os

# import rest in a separate thread
def importRest():
import audio
import socket
thread.start_new_thread(importRest,())

# audio.somemethod() would fail here
===========================


Just import the modules again when you need to use them - modules are
cached, only the initial import is expensive.

Of course this won't help if you don't have something else to do (like
wait for the user) while the threaded imports happen.

Another helpful technique is to put imports inside the functions that
need them. If every module imports the modules it needs at global scope,
then when the first module loads it will trigger importing of the whole
program and all needed library modules. If this is expensive, you can
break up the loads by putting key imports into functions instead of at
global scope.

Note to the doubters - I have used these techniques in Jython programs,
where importing java packages can be noticably slow.

Kent
Feb 25 '06 #8

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

Similar topics

0
by: Atul Kshirsagar | last post by:
Hello, I am embedding python in C++ using SWIG. * The appication runs as a server. * Mutiple clients send requests which execute some python scripts with import statements. * Each request is...
3
by: mh | last post by:
So on most modules I import, I can access the .__file__ attribute to find the implementation. ie: >>> import time >>> time.__file__ '/data1/virtualpython/lib/python2.3/lib-dynload/timemodule.so'...
2
by: Panard | last post by:
Hi, I'm experiencing a strange problem while trying to manage a ftp connection into a separate thread. I'm on linux, python 2.4.3 Here is a test : ------ ftp_thread.py ------ import ftplib...
6
by: robert | last post by:
I get python crashes and (in better cases) strange Python exceptions when (in most cases) importing and using cookielib lazy on demand in a thread. It is mainly with cookielib, but remember the...
11
by: Jim | last post by:
Hi, I have created an import module. And would like to access a function from the main script, e.g., file abc.py: ################### def a(): m() return None
4
by: Earl Anderson | last post by:
I guess I missed the boat on the logic for this one. Immediately upon hitting "Import" in an attempt to import an Excel file containing 7 columns of 'txt' formatted data into AXP, I got a "Type...
3
by: SMALLp | last post by:
Hy! I'm new in Linux, and i feel little less newer in python. I need advice and help. I'm making an application witch purpose is irrelevant. It has a lot of code for now and I've only made...
5
by: George Sakkis | last post by:
I maintain a few configuration files in Python syntax (mainly nested dicts of ints and strings) and use execfile() to read them back to Python. This has been working great; it combines the...
9
by: rsoh.woodhouse | last post by:
Hi, I'm trying to work out some strange (to me) behaviour that I see when running a python script in two different ways (I've inherited some code that needs to be maintained and integrated with...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.