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:
This module implements the second solution.
restarter provides the following members:
| ) |
| ) |
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/
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.