Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32164

RE: resume execution after catching with an excepthook?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feed.xsnews.nl!border-3.ams.xsnews.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ramit.prasad@jpmorgan.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'exception': 0.03; 'debugging': 0.05; 'completeness': 0.07; 'try:': 0.07; 'exit.': 0.09; 'frameworks,': 0.09; 'mode,': 0.09; 'received:155': 0.09; 'snippet': 0.09; 'url:activestate': 0.09; 'anyway': 0.11; 'gui': 0.11; 'accidental': 0.16; 'blocks': 0.16; 'conditional': 0.16; 'disclaimers': 0.16; 'disclaimers,': 0.16; 'from:addr:jpmorgan.com': 0.16; 'oct': 0.16; 'poll': 0.16; 'received:155.180': 0.16; 'received:155.180.234': 0.16; 'received:159.53': 0.16; 'received:bankone.net': 0.16; 'received:exchad.jpmchase.net': 0.16; 'received:jpmchase.com': 0.16; 'received:jpmchase.net': 0.16; 'received:svr.bankone.net': 0.16; 'run.': 0.16; 'securities,': 0.16; 'true:': 0.16; 'try/except': 0.16; 'url:disclosures': 0.16; 'url:jpmorgan': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'thanks,': 0.18; 'to:name:python- list@python.org': 0.20; 'trying': 0.21; 'keyboard': 0.22; 'of.': 0.22; 'ignored.': 0.23; "python's": 0.23; 'received:169.254': 0.24; 'header:In-Reply-To:1': 0.25; '(which': 0.26; 'accuracy': 0.27; 'execution': 0.27; '+0100,': 0.29; 'block,': 0.29; "d'aprano": 0.29; 'loop,': 0.29; 'restart': 0.29; 'steven': 0.29; 'received:169': 0.29; 'url:code': 0.29; 'probably': 0.29; 'maybe': 0.29; 'header:Received:8': 0.30; 'code': 0.31; 'point': 0.31; '(and': 0.32; 'dies': 0.33; 'to:addr:python-list': 0.33; 'likely': 0.33; 'another': 0.33; 'needed': 0.35; 'fail': 0.35; 'subject:?': 0.35; 'something': 0.35; 'there': 0.35; 'next': 0.35; 'really': 0.36; 'except': 0.36; 'but': 0.36; 'depends': 0.36; 'subject:with': 0.36; 'charset:us-ascii': 0.36; 'why': 0.37; 'previous': 0.37; 'subject:: ': 0.38; 'store': 0.38; 'to:addr:python.org': 0.39; 'think': 0.40; 'your': 0.60; 'skip:u 10': 0.60; 'back': 0.62; 'information,': 0.63; 'url:email': 0.63; 'here': 0.65; 'legal': 0.65; 'treat': 0.65; 'subject': 0.66; 'purchase': 0.67; 'believe': 0.69; 'press': 0.71; 'sale': 0.76; 'andrea': 0.84; 'received:169.254.8': 0.84; 'recover': 0.84; 'transactions': 0.91; 'hand,': 0.97
X-DKIM OpenDKIM Filter v2.1.3 sf3.jpmchase.com q9PKxK8j016996
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=jpmorgan.com; s=smtpout; t=1351198760; bh=U0eopSu6u8r4V9/+EcZYOer+dWDWiNJjRH11V6m7zw0=; h=From:To:Subject:Date:Message-ID:References:In-Reply-To: Content-Transfer-Encoding:MIME-Version:Content-Type; b=Slm5CKY3aHIpVnKAzrUQGukF8WM4LZElLo9hFBiF8r4RnAafWIeI/ygR6UPBAnlTJ e2C2KfyZ0p8iGnuENHIk/ognX1G9gDFkM6V5JD1KyUpcJJToZisl8uRHJlapGb/ekU mIBEX8/Y3ScxX/RGH84Y659M4jgbBPPm439qVnh4=
From "Prasad, Ramit" <ramit.prasad@jpmorgan.com>
To "python-list@python.org" <python-list@python.org>
Subject RE: resume execution after catching with an excepthook?
Thread-Topic resume execution after catching with an excepthook?
Thread-Index AQHNsk4tbm5KClblV0qRT+D1aqbMB5fKWCUAgAApbyA=
Date Thu, 25 Oct 2012 20:59:14 +0000
References <mailman.2758.1351083094.27098.python-list@python.org> <5088929e$0$29978$c3e8da3$5496439d@news.astraweb.com> <CAF_E5JYrtsbAzwNcqV0LfFzd82DBZw9oqytNeGxoY8gs-vPLJA@mail.gmail.com>
In-Reply-To <CAF_E5JYrtsbAzwNcqV0LfFzd82DBZw9oqytNeGxoY8gs-vPLJA@mail.gmail.com>
Accept-Language en-US
Content-Language en-US
X-MS-Has-Attach
X-MS-TNEF-Correlator
x-originating-ip [10.67.79.47]
Content-Transfer-Encoding quoted-printable
MIME-Version 1.0
X-DLP-FWD Yes
Content-Type text/plain; charset="us-ascii"
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2866.1351198768.27098.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351198768 news.xs4all.nl 6905 [2001:888:2000:d::a6]:38106
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32164

Show key headers only | View raw


andrea crotti wrote:
> 2012/10/25 Steven D'Aprano <steve+comp.lang.python@pearwood.info>:
> > On Wed, 24 Oct 2012 13:51:30 +0100, andrea crotti wrote:
> >
[snip]
> > Without a try...except block, execution will cease after an exception is
> > caught, even when using sys.excepthook. I don't believe that there is any
> > way to jump back to the line of code that just failed (and why would you,
> > it will just fail again) or the next line (which will likely fail because
> > the previous line failed).
> >
> > I think the only way you can do this is to write your own execution loop:
> >
> > while True:
> >     try:
> >         run(next_command())
> >     except KeyboardInterrupt:
> >         if confirm_quit():
> >             break
> >
> >
> > Of course you need to make run() atomic, or use transactions that can be
> > reverted or backed out of. How plausible this is depends on what you are
> > trying to do -- Python's Ctrl-C is not really designed to be ignored.
> >
> > Perhaps a better approach would be to treat Ctrl-C as an unconditional
> > exit, and periodically poll the keyboard for another key press to use as
> > a conditional exit. Here's a snippet of platform-specific code to get a
> > key press:
> >
> > http://code.activestate.com/recipes/577977
> >
> > Note however that it blocks if there is no key press waiting.
> >
> > I suspect that you may need a proper event loop, as provided by GUI
> > frameworks, or curses.
> >
> 
> Ok thanks, but here the point is not to resume something that is going
> to fail again, just to avoid accidental kill of processes that take a
> long time.  Probably needed only by me in debugging mode, but anyway I
> can do the simple try/except then, thanks..

On the other hand, if you store state externally (pickle?) maybe 
you can just restart at the last "check point". That way even if
the program dies you can recover on the next run.

Ramit Prasad


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

resume execution after catching with an excepthook? andrea crotti <andrea.crotti.0@gmail.com> - 2012-10-24 13:51 +0100
  Re: resume execution after catching with an excepthook? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-25 01:15 +0000
    Re: resume execution after catching with an excepthook? andrea crotti <andrea.crotti.0@gmail.com> - 2012-10-25 15:27 +0100
    Re: resume execution after catching with an excepthook? Chris Angelico <rosuav@gmail.com> - 2012-10-26 01:51 +1100
      Re: resume execution after catching with an excepthook? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-25 15:40 +0000
    RE: resume execution after catching with an excepthook? "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-10-25 20:59 +0000
  Re: resume execution after catching with an excepthook? Hans Mulder <hansmu@xs4all.nl> - 2012-10-25 17:31 +0200
    Re: resume execution after catching with an excepthook? Chris Angelico <rosuav@gmail.com> - 2012-10-26 03:02 +1100

csiph-web