Import in a separate thread

Kent Johnson kent at kentsjohnson.com
Sat Feb 25 10:47:51 EST 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



More information about the Python-list mailing list