Subsections

2.3 restarter -- Automatically restart your program when module files change

This module solves the problem of refreshing Python modules in memory when the source files change, without manually restarting the program. There are two basic ways to solve this problem:

  1. Reload modules within a single process. The basic trick is to delete items from sys.modules, forcing a refresh the next time they are loaded. This gets tough because imported modules depend on each other. Look at Zope and RollbackImporter for two implementations.

  2. Maintain two processes. In this solution, a parent process continuously restarts a child process. This gets around module import dependencies, but the downside is that you loose any program state on restart, and it magnifies the shutdown/start-up time of your program.

This module implements the second solution.

2.3.1 Members

restarter provides the following members:

CHILD, PARENT
These are booleans indicating whether the current process is the parent or child.

launch_child( )
Continuously relaunch the current program in a sub-process until the exit code is something other than 75.

should_restart( )
Return a boolean indicating whether the child process should be restarted. If called in the parent process, it always returns False.

Our implementation uses a thread in the child process (started when the module is imported) to monitor all library source files. Your program is responsible for periodically calling mods_changed, exiting with code 75 whenever it returns True (presumably after cleanly shutting down). Exit code 75 seemed appropriate to use because of its meaning on Unix systems (from /usr/include/sysexits.h, FreeBSD 6.1-RELEASE):

EX_TEMPFAIL -- temporary failure, indicating something that
*              is not really an error.  In sendmail, this means
*              that a mailer (e.g.) could not create a connection,
*              and the request should be reattempted later.
[...]
#define EX_TEMPFAIL     75      /* temp failure; user is invited to retry */

This module requires the subprocess module. Included in the standard library since Python 2.4, it can also be found here:

http://www.lysator.liu.se/~astrand/popen5/


2.3.2 Example

Here is an example:

import restarter

def main():
    while 1:
        # program logic here
        if restarter.should_restart():
            # shutdown code here
            raise SystemExit(75)

if restarter.PARENT:
    restarter.launch_child()
else:
    main()

lib537 is Zeta software. It is copyright © 2006 by Chad Whitacre, and is offered free of charge, warranty, and restrictions.