changeset 607:23467f473eb7

added dbdict-run cmdline option (-2 --allow_sigint) to allow ctrl-c to interrupt progrma
author James Bergstra <bergstrj@iro.umontreal.ca>
date Thu, 15 Jan 2009 22:22:45 -0500
parents e4a92dce13fe
children 8f40262297cf
files pylearn/dbdict/newstuff.py
diffstat 1 files changed, 20 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/pylearn/dbdict/newstuff.py	Thu Jan 15 22:21:35 2009 -0500
+++ b/pylearn/dbdict/newstuff.py	Thu Jan 15 22:22:45 2009 -0500
@@ -247,6 +247,10 @@
         self.state = state
         self.feedback = None
 
+        #TODO: make this a property and disallow changing it during a with block
+        self.catch_sigterm = True
+        self.catch_sigint = True
+
     def switch(self, message = None):
         feedback = self.feedback
         self.feedback = None
@@ -266,7 +270,7 @@
         self.state.dbdict.status = self.RUNNING
 
         v = self.COMPLETE
-        with self:
+        with self: #calls __enter__ and then __exit__
             try:
                 v = self.experiment(self.state, self)
             finally:
@@ -282,10 +286,12 @@
     def __enter__(self):
         # install a SIGTERM handler that asks the experiment function to return
         # the next time it will call switch()
-        self.prev_sigterm = signal.getsignal(signal.SIGTERM)
-        self.prev_sigint = signal.getsignal(signal.SIGINT)
-        signal.signal(signal.SIGTERM, self.on_sigterm)
-        signal.signal(signal.SIGINT, self.on_sigterm)
+        if self.catch_sigterm:
+            self.prev_sigterm = signal.getsignal(signal.SIGTERM)
+            signal.signal(signal.SIGTERM, self.on_sigterm)
+        if self.catch_sigint:
+            self.prev_sigint = signal.getsignal(signal.SIGINT)
+            signal.signal(signal.SIGINT, self.on_sigterm)
         return self
 
     def __exit__(self, type, value, tb_traceback, save = True):
@@ -294,10 +300,12 @@
                 raise type, value, tb_traceback
             except:
                 traceback.print_exc()
-        signal.signal(signal.SIGTERM, self.prev_sigterm)
-        signal.signal(signal.SIGINT, self.prev_sigint)
-        self.prev_sigterm = None
-        self.prev_sigint = None
+        if self.catch_sigterm:
+            signal.signal(signal.SIGTERM, self.prev_sigterm)
+            self.prev_sigterm = None
+        if self.catch_sigint:
+            signal.signal(signal.SIGINT, self.prev_sigint)
+            self.prev_sigint = None
         if save:
             self.save()
         return True
@@ -519,6 +527,8 @@
                           help = 'the working directory in which to run the experiment')
 parser_cmdline.add_option('-n', '--dry-run', action = 'store_true', dest = 'dry_run', default = False,
                           help = 'use this option to run the whole experiment in a temporary working directory (cleaned after use)')
+parser_cmdline.add_option('-2', '--sigint', action = 'store_true', dest = 'allow_sigint', default = False,
+        help = 'allow sigint (CTRL-C) to interrupt a process')
 
 def runner_cmdline(options, experiment, *strings):
     """
@@ -551,6 +561,7 @@
                               experiment, state,
                               redirect_stdout = options.redirect or options.redirect_stdout,
                               redirect_stderr = options.redirect or options.redirect_stderr)
+    channel.catch_sigint = not options.allow_sigint
     channel.run(force = options.force)
     if options.dry_run:
         shutil.rmtree(workdir, ignore_errors=True)