Permalink
Browse files

Only run a single python process. When the config changes just replace

the current process using an execv call.
  • Loading branch information...
1 parent d5c2010 commit fb50f8dc3ba595c246a5366905cbe1b4058a98ca dcurtis committed Aug 18, 2009
Showing with 81 additions and 22 deletions.
  1. +2 −22 bin/caw
  2. +78 −0 bin/cawold
  3. +1 −0 caw/caw.py
View
24 bin/caw
@@ -54,25 +54,5 @@ if __name__ == '__main__':
# save our values
# this acts a wrapper so we can restart our server fresh
- while 1:
- # got this from paster!
- args = [sys.executable, filename]
- #new_environ = os.environ.copy()
- #new_environ[_main_environ] = 'true'
- proc = None
- try:
- #proc = subprocess.Popen(args, env=new_environ)
- proc = subprocess.Popen(args)
- exit_code = proc.wait()
- proc = None
- #print "EXIT ON:", exit_code
- if exit_code != 5:
- break
- finally:
- # proc has not yet been disconnected
- if (proc is not None and hasattr(os, 'kill')):
- import signal
- try:
- os.kill(proc.pid, signal.SIGTERM)
- except (OSError, IOError):
- pass
+ # got this from paster!
+ os.execl(sys.executable, sys.executable, filename)
View
@@ -0,0 +1,78 @@
+#!/usr/bin/python
+
+import os
+import sys
+import subprocess
+
+_main_environ = "CAW_MAIN"
+
+if __name__ == '__main__':
+ # try to load default config from the various xdg directories
+ global_config_dirs = os.path.expandvars('$XDG_CONFIG_DIRS')
+ if global_config_dirs == '$XDG_CONFIG_DIRS':
+ global_config_dirs = ["/etc/xdg",]
+ else:
+ global_config_dirs = global_config_dirs.split(':')
+
+ for dir in global_config_dirs:
+ default_filename = None
+ default_filename = os.path.join(dir, "caw", "config.py")
+ if os.path.isfile(default_filename):
+ break
+
+ #if os.path.isfile(default_filename):
+ #execfile(default_filename, {}, default_config)
+
+ # try to load the user configuration file
+ home_config_dir = os.path.expandvars('$XDG_CONFIG_HOME')
+ if home_config_dir == '$XDG_CONFIG_HOME':
+ home_config_dir = os.path.expanduser("~/.config")
+ user_filename = os.path.join(home_config_dir, "caw", "config.py")
+
+ #user_config = {}
+ #if os.path.isfile(user_filename):
+ #execfile(user_filename, {}, user_config)
+
+ #if not user_config and not default_config:
+ # raise Exception("No config file found")
+
+ #if user_config:
+ # config = user_config
+ # config['config_file']= user_filename
+ #else:
+ # config = default_config
+ # config['config_file']= default_filename
+
+ if os.path.isfile(user_filename):
+ filename = user_filename
+ elif os.path.isfile(default_filename):
+ filename = default_filename
+ else:
+ raise Exception("No Configuration File Found")
+
+ print "Loading Config:", filename
+
+ # save our values
+ # this acts a wrapper so we can restart our server fresh
+ while 1:
+ # got this from paster!
+ args = [sys.executable, filename]
+ #new_environ = os.environ.copy()
+ #new_environ[_main_environ] = 'true'
+ proc = None
+ try:
+ #proc = subprocess.Popen(args, env=new_environ)
+ proc = subprocess.Popen(args)
+ exit_code = proc.wait()
+ proc = None
+ #print "EXIT ON:", exit_code
+ if exit_code != 5:
+ break
+ finally:
+ # proc has not yet been disconnected
+ if (proc is not None and hasattr(os, 'kill')):
+ import signal
+ try:
+ os.kill(proc.pid, signal.SIGTERM)
+ except (OSError, IOError):
+ pass
View
@@ -414,6 +414,7 @@ def mainloop(self):
#if self._mtime is not None and self._mtime < os.path.getmtime(self.config_file):
if os.path.getmtime(sys.argv[0]) > self._mtime:
+ os.execl(sys.executable, sys.executable, *sys.argv)
sys.exit(5)
def schedule(self, timeout, func):

0 comments on commit fb50f8d

Please sign in to comment.