Refinements and tests for cancelling processes

svn: r13719
This commit is contained in:
Doug Blank 2009-12-06 00:36:10 +00:00
parent e686d72a59
commit e091da41de
2 changed files with 34 additions and 6 deletions

View File

@ -124,6 +124,12 @@ class LongOpStatus(Callback):
""" """
return self._secs_left return self._secs_left
def was_cancelled(self):
"""
Has this process been cancelled?
"""
return self._cancel
def cancel(self): def cancel(self):
"""Inform the operation that it should complete. """Inform the operation that it should complete.
""" """
@ -186,7 +192,7 @@ class LongOpStatus(Callback):
if __name__ == '__main__': if __name__ == '__main__':
s = LongOpStatus("msg", 100, 10) s = LongOpStatus("msg", 100, 10, can_cancel=True)
def heartbeat(): def heartbeat():
print "heartbeat ", s.estimated_secs_to_complete() print "heartbeat ", s.estimated_secs_to_complete()
@ -197,9 +203,20 @@ if __name__ == '__main__':
s.connect('op-heartbeat', heartbeat) s.connect('op-heartbeat', heartbeat)
s.connect('op-end', end) s.connect('op-end', end)
import signal
def ctrlc_handler(signum, frame):
print "Received interrupt!"
s.cancel()
signal.signal(signal.SIGINT, ctrlc_handler)
for i in xrange(0, 99): for i in xrange(0, 99):
if s.should_cancel():
break
time.sleep(0.1) time.sleep(0.1)
s.heartbeat() s.heartbeat()
if not s.was_cancelled():
s.end() s.end()

View File

@ -126,6 +126,10 @@ class ProgressMonitor(object):
log.debug("heartbeat in ProgressMonitor") log.debug("heartbeat in ProgressMonitor")
if idx >= len(self._status_stack):
# this item has been cancelled
return
facade = self._status_stack[idx] facade = self._status_stack[idx]
if facade.status_obj.estimated_secs_to_complete() > self._popup_time: if facade.status_obj.estimated_secs_to_complete() > self._popup_time:
@ -143,8 +147,15 @@ class ProgressMonitor(object):
def _end(self, idx): def _end(self, idx):
# hide any progress dialog # hide any progress dialog
# remove the status object from the stack # remove the status object from the stack
log.debug("received end in ProgressMonitor") log.debug("received end in ProgressMonitor")
if idx >= len(self._status_stack):
# this item has been cancelled
return
while idx < len(self._status_stack) - 1:
self._end(len(self._status_stack) - 1)
facade = self._status_stack[idx] facade = self._status_stack[idx]
if facade.active: if facade.active:
dlg = self._get_dlg() dlg = self._get_dlg()