Will Python execute finally block after receiving Ctrl+C -


if stop python script ctrl+c, execute blocks, or literally stop script is?

well, answer it depends. here happens:

  • python executes code in try:... finally: block
  • a ctrl-c emitted , translated in keyboardinterrupt exception
  • processing interrupted , controls passes block

so @ first sight, works expected. but...

when user (not you, others...) wants interrupt task hits multiple times ctrl-c. first 1 branch execution in finally block. if ctrl-c occurs in middle of block because contains slow operations closing files, new keyboardinterrupt raised , nothing guarantees whole block executed, , have like:

traceback (most recent call last):   file "...", line ..., in ...     ... keyboardinterrupt  during handling of above exception, exception occurred:  traceback (most recent call last):   file "...", line ..., in ...     ...   file "...", line ..., in ...     ... keyboardinterrupt 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -