我刚收到以下错误,我不知道该怎么做。
Unhandled exception in thread started by <bound method Timer.__bootstrap of <Timer(Thread-3, stopped -1234564240)>>
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 525, in __bootstrap
self.__bootstrap_inner()
File "/usr/lib/python2.7/threading.py", line 565, in __bootstrap_inner
(self.name, _format_exc()))
File "/usr/lib/python2.7/traceback.py", line 241, in format_exc
return ''.join(format_exception(etype, value, tb, limit))
File "/usr/lib/python2.7/traceback.py", line 141, in format_exception
list = list + format_tb(tb, limit)
File "/usr/lib/python2.7/traceback.py", line 76, in format_tb
return format_list(extract_tb(tb, limit))
File "/usr/lib/python2.7/traceback.py", line 101, in extract_tb
line = linecache.getline(filename, lineno, f.f_globals)
File "/usr/lib/python2.7/linecache.py", line 14, in getline
lines = getlines(filename, module_globals)
File "/usr/lib/python2.7/linecache.py", line 40, in getlines
return updatecache(filename, module_globals)
File "/usr/lib/python2.7/linecache.py", line 133, in updatecache
lines = fp.readlines()
MemoryError
相关代码(虽然我不确定它是否真的相关 - 但它只是我的代码中唯一以异常方式提及的部分):
class Timer(threading.Thread):
def __init__(self, interval, callback, limit=0, args=[], kwargs={}):
threading.Thread.__init__(self)
self.interval = interval / 1000.0
self.callback = callback
self.limit = limit
self.args = args
self.kwargs = kwargs
self.iterations = 0
self._stop = threading.Event()
def restart(self):
self.iterations = 0
self._stop.clear()
threading.Thread.__init__(self)
self.start()
def run(self):
while not self._stop.wait(self.interval):
self.callback(*self.args, **self.kwargs)
self.iterations += 1
if self.limit > 0 and self.iterations >= self.limit:
break
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
我认为大概是在这个时候我运行代码的服务器有点崩溃 - 这只是一个症状,还是表明我的代码在其他地方出现了问题? 但是,大多数情况下,我只是想知道这到底是什么意思,我可以理解其余部分。
答案 0 :(得分:2)
你内存不足。来自python docs on exceptions:
异常MemoryError
当操作耗尽内存时引发但是 情况可能仍然得到拯救(通过删除一些对象)。该 关联值是一个字符串,表示什么样的(内部) 操作耗尽内存。请注意,因为底层 内存管理架构(C的malloc()函数),. 口译员可能无法始终完全从中恢复 情况;然而,它引发了一个异常以便堆叠 如果出现失控程序,可以打印回溯。
所以你要么:
请注意, 32位系统上的Python具有2G内存限制,无论您拥有多少物理内存,或者启用了PAE。这不是特定于Python的 - 这是操作系统限制。
可能不是导致问题的Timer
类 - 只是因为发生而在使用Timer
做某事时耗尽了内存。
答案 1 :(得分:0)
来自Python Docs ...
exception MemoryError
Raised when an operation runs out of memory but the situation may still be rescued
(by deleting some objects). The associated value is a string indicating what kind
of (internal) operation ran out of memory. Note that because of the underlying
memory management architecture (C’s malloc() function), the interpreter may not
always be able to completely recover from this situation; it nevertheless raises
an exception so that a stack traceback can be printed, in case a run-away program
was the cause.