reimport module every n seconds

Roy Smith roy at panix.com
Thu Feb 17 19:14:22 CET 2011


In article <ijjo0p$3dm$1 at panix5.panix.com>, aahz at pythoncraft.com (Aahz) 
wrote:

> In article 
> <6cde71c6-5c56-40ea-9849-50fc44e5decc at o14g2000prb.googlegroups.com>,
> Santiago Caracol  <santiago.caracol at gmail.com> wrote:
> >
> >a server program of mine uses data which are compiled to a Python
> >module for efficiency reasons. In some module of the server program I
> >import the data:
> >
> >from data import data
> >
> >As the data often changes, I would like to reimport it every n (e.g.
> >10) seconds.
> 
> Don't do that.  ;-)  I suggest using exec instead.  However, I would be
> surprised if import worked faster than, say, JSON (more precisely, I
> doubt that it's enough faster to warrnat this kludge).

I'm with Aahz.  Don't do that.

I don't know what you're doing, but I suspect an even better solution 
would be to have your program run a "reconfigure" thread which listens 
on a UDP socket and reads a JSON object from it.  Or, at the very least, 
which listens for a signal which kicks it to re-read some JSON from a 
disk file.  This will be more responsive when the data changes quickly 
and more efficient when it changes slowly.  As opposed to just polling 
for changes every 10 seconds.



More information about the Python-list mailing list