osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Suggestions on mechanism or existing code - maintain persistence of file download history


On Sat, Feb 1, 2020 at 4:17 PM DL Neil via Python-list
<python-list at python.org> wrote:
>
> On 31/01/20 9:53 PM, R.Wieser wrote:
> >> Using ctrl+c is a VERY BAD idea.
> >
> > To have it just exit the program ?  Yes, indeed.
> >
> > Though you /could/ keep track of what needs to be finished and have the
> > ctrl-c handler do that for you (barf).
> >
> > Another posibility is to capture the ctrl-c and set a flag, which than
> > instructs the program to terminate loops wherever possible - and thus have
> > the program finish as if there was no more to do.
> >
> >> (see also 'sledgehammer to crack a nut')
> >
> > While I agree with you there, I've been searching for other ways to detect a
> > keypress (in a console-based script) and have found none.   IOW, you do not
> > (seem to) have another option.
>
> Color me disappointed! I was hoping to be enlightened as to how one
> might code exactly that. (so I went looking...)
>
> My first thought was a try...except looking for a keyboard error, but
> wouldn't that only work if there was an input() loop? The idea of
> putting, effectively the entire code-base, into a try...except block
> seemed crude and likely to cause side-effects.

No, it should be fine - you'll get KeyboardInterrupt even without
input(). At least, you will on Unix systems; can someone confirm that
this is also the case on Windows?

Having a try/except around your main function that catches one
specific exception is usually fine. I often do this in the main of
something that's designed to run a server - for development, I'll
invoke the script directly and then Ctrl-C to stop it, for deployment,
it'll be run through some service manager.

> Sigh, if I had a dollar for every time someone looked at a simple
> solution (MS-Excel I'm looking at you!) and uttered 'suggestions' such
> as "we could run the entire company off one of these spreadsheet things"
> or "if we captured all the company's transactions, couldn't we run it
> off one Python pgm?"...

... ugh, I feel your pain... yep, heard that sort of thing way too many times.

ChrisA