Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Import in a separate thread

Reply
Thread Tools

Import in a separate thread

 
 
cyberco
Guest
Posts: n/a
 
      02-25-2006
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
===========================

 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      02-25-2006
"cyberco" <> 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.
 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      02-25-2006
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

 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      02-25-2006
Peter Hansen <> 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.
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      02-25-2006
Paul Rubin wrote:
> Peter Hansen <> 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

 
Reply With Quote
 
cyberco
Guest
Posts: n/a
 
      02-25-2006
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.

 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      02-25-2006
On 2006-02-25, cyberco <> 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!!
 
Reply With Quote
 
Kent Johnson
Guest
Posts: n/a
 
      02-25-2006
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
 
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
multithreading with each thread in a separate class instance vs. in aC-style kernel thread calls in C++ ssylee C++ 2 01-28-2008 09:39 AM
Separate Tabs, Separate Sessions BigAndy Firefox 0 05-09-2007 09:27 AM
Separate Tabs, Separate Sessions BigAndy Firefox 0 05-09-2007 09:26 AM
Using separate classpaths for separate classes? Frank Fredstone Java 1 06-27-2006 06:46 AM
How to use several separate classes (separate files) to be executed in one class (another file) EvgueniB Java 1 12-15-2003 01:18 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