Can somebody give me an advice about what to learn?

Roy Smith roy at panix.com
Sun Sep 30 11:01:10 EDT 2012


In article <mailman.1675.1349015755.27098.python-list at python.org>,
 Chris Angelico <rosuav at gmail.com> wrote:

> On Mon, Oct 1, 2012 at 12:23 AM, Roy Smith <roy at panix.com> wrote:
> > In article <mailman.1672.1349011558.27098.python-list at python.org>,
> >  Chris Angelico <rosuav at gmail.com> wrote:
> >
> >> there's no efficient and reliable way to change/reload code in a
> >> running application (not often an issue).
> >
> > What we do (largely cribbed from django's runserver) is start up a
> > thread which once a second, looks at all the modules in sys.modules,
> > checks to see if the modification time for their source files has
> > changed, and if so, restarts the application.  This is hugely convenient
> > when developing any kind of long-running application.  You don't need to
> > keep restarting the application; just edit the source and changes take
> > effect (almost) immediately.
> >
> > Not sure if this is what you had in mind.
> 
> It's not an _explicit_ restart, but you have to write your application
> to keep all its state on disk in some way.

Well, more strictly, what you need is to keep your state somewhere else.  
Doesn't have to be on disk.  Could be in memory, if that memory belongs 
to another process (memcache, redis, or any of a number of in-memory 
databases).

> What I'm talking about is
> having a single process that never terminates, never stops accepting
> connections, but at some point new connections begin to be served with
> new code - with old ones, if they're still going, continuing to be
> handled by the old code. I have one such process that's been going for
> (let me check) 115 wk 0d 21:05:21.

Why does it have to be a single process?  I could imagine some front-end 
process which accepts connections and hands them off to worker 
processes.  When you install new code, you could do it in a different 
directory tree, and then signal the front end that new code exists.  The 
front end could then {chdir, munge sys.path, whatever} to the root of 
the new tree and keep going.  Existing connections stay up with the old 
code, new connections get the new code.

If you didn't want to fork a new process every time, you could have a 
worker process pool.  When a worker signaled it was done with a task, 
the front end could either assign it a new connection (if the code it 
was running was still current), or kill it off if it was running stale 
code.

If you truly needed this to be a single process, I could imagine a 
customized module importer which altered the module name to include a 
version prefix before registering it in sys.modules.  I think that could 
be made to work, but would be really ugly and complicated.  Or elegant 
and nifty, depending on your attitude :-)



More information about the Python-list mailing list