Catching only *one* KeyboardInterruptExceptionCatch multiple exceptions at once?The case against checked exceptionsGlobally catch exceptions in a WPF application?Catching / blocking SIGINT during system callCan I catch multiple Java exceptions in the same catch clause?Catch multiple exceptions in one line (except block)grep, but only certain file extensionsa custom interrupt handler for mpirunHow to ignore keyboardInterrupt Exception in a module and deal with it in higher level in winpexpet?python picamera, keyboard ctrl+c/sigint not caught

Could the terminal length of components like resistors be reduced?

How did Captain America manage to do this?

What's the polite way to say "I need to urinate"?

What is the most expensive material in the world that could be used to create Pun-Pun's lute?

Relationship between strut and baselineskip

555 timer FM transmitter

How does Captain America channel this power?

a sore throat vs a strep throat vs strep throat

Is it idiomatic to construct against `this`

"Whatever a Russian does, they end up making the Kalashnikov gun"? Are there any similar proverbs in English?

What's the name of these pliers?

How come there are so many candidates for the 2020 Democratic party presidential nomination?

Can SQL Server create collisions in system generated constraint names?

Why do games have consumables?

Why didn't the Space Shuttle bounce back into space as many times as possible so as to lose a lot of kinetic energy up there?

How do I deal with a coworker that keeps asking to make small superficial changes to a report, and it is seriously triggering my anxiety?

acheter à, to mean both "from" and "for"?

How do I reattach a shelf to the wall when it ripped out of the wall?

Does a large simulator bay have standard public address announcements?

Why was the Spitfire's elliptical wing almost uncopied by other aircraft of World War 2?

Implications of cigar-shaped bodies having rings?

I preordered a game on my Xbox while on the home screen of my friend's account. Which of us owns the game?

How do I check if a string is entirely made of the same substring?

How to fry ground beef so it is well-browned



Catching only *one* KeyboardInterruptException


Catch multiple exceptions at once?The case against checked exceptionsGlobally catch exceptions in a WPF application?Catching / blocking SIGINT during system callCan I catch multiple Java exceptions in the same catch clause?Catch multiple exceptions in one line (except block)grep, but only certain file extensionsa custom interrupt handler for mpirunHow to ignore keyboardInterrupt Exception in a module and deal with it in higher level in winpexpet?python picamera, keyboard ctrl+c/sigint not caught






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have a long-running task that may be interrupted because an exception is raised inside it, or because Control+C is pressed signaling a SIGINT, raising a KeyboardInterruptException.



In both of these cases, the path to follow is to store the results that are already processed by the task, to prevent the lose of the computing time. This store takes some time, as it may need to process a good amount of information. The problem appears when Control+C is pressed again when the interrupt has already been caught.



Example:



task = SomeTask()
try:
task.start()
except KeyboardInterruptException:
print("Keyboard interrupted")
except Exception as e:
print_exception(e) # To show what happened
finally:
task.store_results() # If Control-C is pressed here, data gets corrupted


I need a way to catch the interrupt, launch the store process and prevent another interrupt from happening.










share|improve this question
























  • Suggest you add and OS tag to your question—because handling SIGINT varies depending on what operating system you're using.

    – martineau
    Mar 22 at 18:15

















0















I have a long-running task that may be interrupted because an exception is raised inside it, or because Control+C is pressed signaling a SIGINT, raising a KeyboardInterruptException.



In both of these cases, the path to follow is to store the results that are already processed by the task, to prevent the lose of the computing time. This store takes some time, as it may need to process a good amount of information. The problem appears when Control+C is pressed again when the interrupt has already been caught.



Example:



task = SomeTask()
try:
task.start()
except KeyboardInterruptException:
print("Keyboard interrupted")
except Exception as e:
print_exception(e) # To show what happened
finally:
task.store_results() # If Control-C is pressed here, data gets corrupted


I need a way to catch the interrupt, launch the store process and prevent another interrupt from happening.










share|improve this question
























  • Suggest you add and OS tag to your question—because handling SIGINT varies depending on what operating system you're using.

    – martineau
    Mar 22 at 18:15













0












0








0








I have a long-running task that may be interrupted because an exception is raised inside it, or because Control+C is pressed signaling a SIGINT, raising a KeyboardInterruptException.



In both of these cases, the path to follow is to store the results that are already processed by the task, to prevent the lose of the computing time. This store takes some time, as it may need to process a good amount of information. The problem appears when Control+C is pressed again when the interrupt has already been caught.



Example:



task = SomeTask()
try:
task.start()
except KeyboardInterruptException:
print("Keyboard interrupted")
except Exception as e:
print_exception(e) # To show what happened
finally:
task.store_results() # If Control-C is pressed here, data gets corrupted


I need a way to catch the interrupt, launch the store process and prevent another interrupt from happening.










share|improve this question
















I have a long-running task that may be interrupted because an exception is raised inside it, or because Control+C is pressed signaling a SIGINT, raising a KeyboardInterruptException.



In both of these cases, the path to follow is to store the results that are already processed by the task, to prevent the lose of the computing time. This store takes some time, as it may need to process a good amount of information. The problem appears when Control+C is pressed again when the interrupt has already been caught.



Example:



task = SomeTask()
try:
task.start()
except KeyboardInterruptException:
print("Keyboard interrupted")
except Exception as e:
print_exception(e) # To show what happened
finally:
task.store_results() # If Control-C is pressed here, data gets corrupted


I need a way to catch the interrupt, launch the store process and prevent another interrupt from happening.







python unix exception keyboardinterrupt






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 19:16







0xfede7c8

















asked Mar 22 at 17:36









0xfede7c80xfede7c8

31




31












  • Suggest you add and OS tag to your question—because handling SIGINT varies depending on what operating system you're using.

    – martineau
    Mar 22 at 18:15

















  • Suggest you add and OS tag to your question—because handling SIGINT varies depending on what operating system you're using.

    – martineau
    Mar 22 at 18:15
















Suggest you add and OS tag to your question—because handling SIGINT varies depending on what operating system you're using.

– martineau
Mar 22 at 18:15





Suggest you add and OS tag to your question—because handling SIGINT varies depending on what operating system you're using.

– martineau
Mar 22 at 18:15












1 Answer
1






active

oldest

votes


















1














Just set the signal handler:



import signal

task = SomeTask()
try:
task.start()
except KeyboardInterruptException:
print("Keyboard interrupted")
except Exception as e:
print_exception(e) # To show what happened
finally:
signal.signal(signal.SIGINT, lamda *_: print('Wait!'))
task.store_results() # If Control-C is pressed here, data gets corrupted


Source: https://pythonadventures.wordpress.com/2012/11/21/handle-ctrlc-in-your-script/






share|improve this answer























  • This did the trick for me. It has less side effects because the first time it enters through the try/except and then it disables the interrupt :) Thanks

    – 0xfede7c8
    Mar 22 at 19:17











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55305047%2fcatching-only-one-keyboardinterruptexception%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Just set the signal handler:



import signal

task = SomeTask()
try:
task.start()
except KeyboardInterruptException:
print("Keyboard interrupted")
except Exception as e:
print_exception(e) # To show what happened
finally:
signal.signal(signal.SIGINT, lamda *_: print('Wait!'))
task.store_results() # If Control-C is pressed here, data gets corrupted


Source: https://pythonadventures.wordpress.com/2012/11/21/handle-ctrlc-in-your-script/






share|improve this answer























  • This did the trick for me. It has less side effects because the first time it enters through the try/except and then it disables the interrupt :) Thanks

    – 0xfede7c8
    Mar 22 at 19:17















1














Just set the signal handler:



import signal

task = SomeTask()
try:
task.start()
except KeyboardInterruptException:
print("Keyboard interrupted")
except Exception as e:
print_exception(e) # To show what happened
finally:
signal.signal(signal.SIGINT, lamda *_: print('Wait!'))
task.store_results() # If Control-C is pressed here, data gets corrupted


Source: https://pythonadventures.wordpress.com/2012/11/21/handle-ctrlc-in-your-script/






share|improve this answer























  • This did the trick for me. It has less side effects because the first time it enters through the try/except and then it disables the interrupt :) Thanks

    – 0xfede7c8
    Mar 22 at 19:17













1












1








1







Just set the signal handler:



import signal

task = SomeTask()
try:
task.start()
except KeyboardInterruptException:
print("Keyboard interrupted")
except Exception as e:
print_exception(e) # To show what happened
finally:
signal.signal(signal.SIGINT, lamda *_: print('Wait!'))
task.store_results() # If Control-C is pressed here, data gets corrupted


Source: https://pythonadventures.wordpress.com/2012/11/21/handle-ctrlc-in-your-script/






share|improve this answer













Just set the signal handler:



import signal

task = SomeTask()
try:
task.start()
except KeyboardInterruptException:
print("Keyboard interrupted")
except Exception as e:
print_exception(e) # To show what happened
finally:
signal.signal(signal.SIGINT, lamda *_: print('Wait!'))
task.store_results() # If Control-C is pressed here, data gets corrupted


Source: https://pythonadventures.wordpress.com/2012/11/21/handle-ctrlc-in-your-script/







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 22 at 17:50









ADRADR

767315




767315












  • This did the trick for me. It has less side effects because the first time it enters through the try/except and then it disables the interrupt :) Thanks

    – 0xfede7c8
    Mar 22 at 19:17

















  • This did the trick for me. It has less side effects because the first time it enters through the try/except and then it disables the interrupt :) Thanks

    – 0xfede7c8
    Mar 22 at 19:17
















This did the trick for me. It has less side effects because the first time it enters through the try/except and then it disables the interrupt :) Thanks

– 0xfede7c8
Mar 22 at 19:17





This did the trick for me. It has less side effects because the first time it enters through the try/except and then it disables the interrupt :) Thanks

– 0xfede7c8
Mar 22 at 19:17



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55305047%2fcatching-only-one-keyboardinterruptexception%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript