Import in a separate thread

Paul Rubin http
Sat Feb 25 07:17:51 EST 2006


"cyberco" <cyberco at 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.  



More information about the Python-list mailing list