欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python捕獲全局的KeyboardInterrupt異常的方法實(shí)現(xiàn)

 更新時(shí)間:2024年08月20日 10:37:21   作者:Looooking  
KeyboardInterrupt異常是Python中的一個(gè)標(biāo)準(zhǔn)異常,它通常發(fā)生在用戶(hù)通過(guò)鍵盤(pán)中斷了一個(gè)正在運(yùn)行的程序,本文主要介紹了Python捕獲全局的KeyboardInterrupt異常的方法實(shí)現(xiàn),感興趣的可以了解一下

當(dāng)然,像下面這種情況。

你要是把所有代碼像下面那樣都放到 try, except 的情況,就當(dāng)我什么也沒(méi)說(shuō)。

import time

def main():
    print('before ...')
    time.sleep(10)
    print('after ...')


if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print('\nKeyboardInterrupt ...')
    print('the end')
root@master ~/w/python3_learning# python3 test.py 
before ...
^C
KeyboardInterrupt ...
the end

一般情況下,程序運(yùn)行過(guò)程當(dāng)中要執(zhí)行的代碼量會(huì)比較大,一般用戶(hù)執(zhí)行 Ctrl + C 程序就報(bào)錯(cuò) KeyboardInterrupt 停止了。

import time

def main():
    print('before ...')
    time.sleep(10)
    print('after ...')


if __name__ == '__main__':
    main()
    print('the end')
root@master ~/w/python3_learning# python3 test.py 
before ...
^CTraceback (most recent call last):
  File "test.py", line 11, in <module>
    main()
  File "test.py", line 6, in main
    time.sleep(10)
KeyboardInterrupt

但是有時(shí)候,我們希望用戶(hù)在 Ctrl + C 之后再繼續(xù)執(zhí)行一些清理操作。

import sys
import time


def suppress_keyboard_interrupt_message():
    old_excepthook = sys.excepthook

    def new_hook(exctype, value, traceback):
        if exctype != KeyboardInterrupt:
            old_excepthook(exctype, value, traceback)
        else:
            print('\nKeyboardInterrupt ...')
            print('do something after Interrupt ...')
    sys.excepthook = new_hook


def main():
    print('before ...')
    time.sleep(10)
    print('after ...')


if __name__ == '__main__':
    suppress_keyboard_interrupt_message()
    main()
    print('the end')
root@master ~/w/python3_learning# python3 test.py 
before ...
^C
KeyboardInterrupt ...
do something after Interrupt ...

由于 suppress_keyboard_interrupt_message 函數(shù)中的 new_hook 是自定義的,所以你也不一定局限于只處理某種異常,甚至對(duì)所有異常做統(tǒng)一處理也是可以的。

到此這篇關(guān)于Python捕獲全局的KeyboardInterrupt異常的方法實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python KeyboardInterrupt異常內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python在CMD界面讀取excel所有數(shù)據(jù)的示例

    python在CMD界面讀取excel所有數(shù)據(jù)的示例

    這篇文章主要介紹了python在CMD界面讀取excel所有數(shù)據(jù),幫助大家更好的利用python辦公,感興趣的朋友可以了解下
    2020-09-09
  • Python 含參構(gòu)造函數(shù)實(shí)例詳解

    Python 含參構(gòu)造函數(shù)實(shí)例詳解

    這篇文章主要介紹了Python 含參構(gòu)造函數(shù)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Pycharm 解決自動(dòng)格式化沖突的設(shè)置操作

    Pycharm 解決自動(dòng)格式化沖突的設(shè)置操作

    這篇文章主要介紹了Pycharm 解決自動(dòng)格式化沖突的設(shè)置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • Python全局變量用法實(shí)例分析

    Python全局變量用法實(shí)例分析

    這篇文章主要介紹了Python全局變量用法,結(jié)合實(shí)例形式分析了Python中全局變量的定義、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2016-07-07
  • Python中的列表及其操作方法

    Python中的列表及其操作方法

    這篇文章主要介紹了Python中的列表及其操作方法,涉及到的方法包括對(duì)列表元素進(jìn)行修改、添加、刪除、排序以及求列表長(zhǎng)度等,此外還介紹了列表的遍歷、數(shù)值列表、切片和元組的一些操作,下文詳細(xì)介紹需要的小伙伴可以參考一下
    2022-03-03
  • 利用python求積分的實(shí)例

    利用python求積分的實(shí)例

    今天小編就為大家分享一篇利用python求積分的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-07-07
  • python動(dòng)態(tài)視頻下載器的實(shí)現(xiàn)方法

    python動(dòng)態(tài)視頻下載器的實(shí)現(xiàn)方法

    這里向大家分享一下python爬蟲(chóng)的一些應(yīng)用,主要是用爬蟲(chóng)配合簡(jiǎn)單的GUI界面實(shí)現(xiàn)視頻,音樂(lè)和小說(shuō)的下載器。今天就先介紹如何實(shí)現(xiàn)一個(gè)動(dòng)態(tài)視頻下載器,需要的朋友可以參考下
    2019-09-09
  • python實(shí)現(xiàn)FTP服務(wù)器服務(wù)的方法

    python實(shí)現(xiàn)FTP服務(wù)器服務(wù)的方法

    本篇文章主要介紹了python實(shí)現(xiàn)FTP服務(wù)器的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-04-04
  • pandas 數(shù)據(jù)歸一化以及行刪除例程的方法

    pandas 數(shù)據(jù)歸一化以及行刪除例程的方法

    今天小編就為大家分享一篇pandas 數(shù)據(jù)歸一化以及行刪除例程的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • python 借助numpy保存數(shù)據(jù)為csv格式的實(shí)現(xiàn)方法

    python 借助numpy保存數(shù)據(jù)為csv格式的實(shí)現(xiàn)方法

    今天小編就為大家分享一篇python 借助numpy保存數(shù)據(jù)為csv格式的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07

最新評(píng)論