我有一個python腳本,包含一個大循環(huán)讀取文件和做一些東西(我使用幾個包,如urllib2,httplib2或BeautifulSoup).
它看起來像這樣:
try:
with open(fileName, 'r') as file :
for i, line in enumerate(file):
try:
# a lot of code
# ....
# ....
except urllib2.HTTPError:
print "\n >>> HTTPError"
# a lot of other exceptions
# ....
except (KeyboardInterrupt, SystemExit):
print "Process manually stopped"
raise
except Exception, e:
print(repr(e))
except (KeyboardInterrupt, SystemExit):
print "Process manually stopped"
# some stuff
問題是,當我按下Ctrl-C時程序停止但是它沒有被我的兩個KeyboardInterrupt異常中的任何一個捕獲,雖然我確定它當前在循環(huán)中(因此至少在big try / except中).
怎么可能?起初我以為是因為我正在使用的其中一個包沒有正確處理異常(例如只使用“except:”),但如果是這樣,我的腳本就不會停止.但腳本會停止,它應該被至少一個我的兩個捕獲,除了,對吧?
我哪里錯了?
提前致謝!
編輯:
通過在try-except之后添加finally:子句并在兩個try-except塊中打印回溯,當我按下Ctrl-C時它通常顯示None,但我曾經(jīng)設法得到它(似乎它來自urllib2,但我不知道是不是我無法捕獲KeyboardInterrupt的原因:
Traceback(最近一次調(diào)用最后一次):
File "/home/darcot/code/Crawler/crawler.py", line 294, in get_articles_from_file
content = Extractor(extractor='ArticleExtractor', url=url).getText()
File "/usr/local/lib/python2.7/site-packages/boilerpipe/extract/__init__.py", line 36, in __init__
connection = urllib2.urlopen(request)
File "/usr/local/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/local/lib/python2.7/urllib2.py", line 391, in open
response = self._open(req, data)
File "/usr/local/lib/python2.7/urllib2.py", line 409, in _open
'_open', req)
File "/usr/local/lib/python2.7/urllib2.py", line 369, in _call_chain
result = func(*args)
File "/usr/local/lib/python2.7/urllib2.py", line 1173, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/local/lib/python2.7/urllib2.py", line 1148, in do_open
raise URLError(err)
URLError: <urlopen error [Errno 4] Interrupted system call>
解決方法: 我已經(jīng)在我的評論中提出了這個問題,這個問題可能是由問題中遺漏的代碼部分引起的.但是,確切的代碼不應該是相關(guān)的,因為當Python代碼被Ctrl-C中斷時,Python通常會拋出KeyboardInterrupt異常.
您在評論中提到使用了samppipe Python包.這個Python包使用JPype來創(chuàng)建綁定到Java的語言…我可以使用以下Python程序重現(xiàn)您的問題:
from boilerpipe.extract import Extractor
import time
try:
for i in range(10):
time.sleep(1)
except KeyboardInterrupt:
print "Keyboard Interrupt Exception"
如果使用Ctrl-C中斷此程序,則不會拋出異常.似乎程序立即終止,而Python解釋器沒有機會拋出異常.當刪除導管的導入時,問題就消失了……
與gdb的調(diào)試會話表明,如果導入了boilerpipe,Python會啟動大量線程:
gdb --args python boilerpipe_test.py
[...]
(gdb) run
Starting program: /home/fabian/Experimente/pykeyinterrupt/bin/python boilerpipe_test.py
warning: Could not load shared library symbols for linux-vdso.so.1.
Do you need "set solib-search-path" or "set sysroot"?
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
[New Thread 0x7fffef62b700 (LWP 3840)]
[New Thread 0x7fffef52a700 (LWP 3841)]
[New Thread 0x7fffef429700 (LWP 3842)]
[New Thread 0x7fffef328700 (LWP 3843)]
[New Thread 0x7fffed99a700 (LWP 3844)]
[New Thread 0x7fffed899700 (LWP 3845)]
[New Thread 0x7fffed798700 (LWP 3846)]
[New Thread 0x7fffed697700 (LWP 3847)]
[New Thread 0x7fffed596700 (LWP 3848)]
[New Thread 0x7fffed495700 (LWP 3849)]
[New Thread 0x7fffed394700 (LWP 3850)]
[New Thread 0x7fffed293700 (LWP 3851)]
[New Thread 0x7fffed192700 (LWP 3852)]
沒有samppipe導入的gdb會話:
gdb --args python boilerpipe_test.py
[...]
(gdb) r
Starting program: /home/fabian/Experimente/pykeyinterrupt/bin/python boilerpipe_test.py
warning: Could not load shared library symbols for linux-vdso.so.1.
Do you need "set solib-search-path" or "set sysroot"?
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
^C
Program received signal SIGINT, Interrupt.
0x00007ffff7529533 in __select_nocancel () from /usr/lib/libc.so.6
(gdb) signal 2
Continuing with signal SIGINT.
Keyboard Interrupt Exception
[Inferior 1 (process 3904) exited normally
因此,我假設您的Ctrl-C信號在另一個線程中處理,或者jpype執(zhí)行其他破壞Ctrl-C處理的奇怪事情.
編輯:作為一種可能的解決方法,您可以注冊一個信號處理程序,捕獲當您按Ctrl-C時進程收到的SIGINT信號.即使導入了boilerpipe和JPype,信號處理程序也會被觸發(fā).這樣,當用戶按下Ctrl-C時,您將收到通知,并且您將能夠在程序的中心點處理該事件.如果要在此處理程序中,可以終止腳本.如果不這樣做,腳本將在信號處理函數(shù)返回后繼續(xù)運行.請參閱以下示例:
from boilerpipe.extract import Extractor
import time
import signal
import sys
def interuppt_handler(signum, frame):
print "Signal handler!!!"
sys.exit(-2) #Terminate process here as catching the signal removes the close process behaviour of Ctrl-C
signal.signal(signal.SIGINT, interuppt_handler)
try:
for i in range(10):
time.sleep(1)
# your_url = "http://www."
# extractor = Extractor(extractor='ArticleExtractor', url=your_url)
except KeyboardInterrupt:
print "Keyboard Interrupt Exception"
來源:https://www./content-1-293401.html
|