我用 try/except 块包装了一些内存不足的代码。但是,虽然生成了 MemoryError,但它并没有被捕获。
我有以下代码:
while True:
try:
self.create_indexed_vocab( vocab )
self.reset_weights()
break;
except MemoryError:
# Stuff to reduce size of vocabulary
self.vocab, self.index2word = None, None
self.syn0, self.syn1 = None, None
self.min_count += 1
logger.info( ...format string here... )
我得到以下回溯:
File "./make_model_tagged_wmt11.py", line 39, in <module>
model.build_vocab(sentences)
File "/root/CustomCompiledSoftware/gensim/gensim/models/word2vec.py", line 236, in build_vocab
self.reset_weights()
File "/root/CustomCompiledSoftware/gensim/gensim/models/word2vec.py", line 347, in reset_weights
self.syn0 += (random.rand(len(self.vocab), self.layer1_size) - 0.5) / self.layer1_size
File "mtrand.pyx", line 1044, in mtrand.RandomState.rand (numpy/random/mtrand/mtrand.c:6523)
File "mtrand.pyx", line 760, in mtrand.RandomState.random_sample (numpy/random/mtrand/mtrand.c:5713)
File "mtrand.pyx", line 137, in mtrand.cont0_array (numpy/random/mtrand/mtrand.c:1300)
MemoryError
我在 Ubuntu 12.04 下运行 Python 2.7.3
该reset_weights
行self.syn0
正是我期望引发异常的行(它分配了一个大数组)。令人费解的是,我无法捕捉到内存错误并做一些使数组变小的事情。
是否有特殊情况导致MemoryError
无法被抓?