我有一些使用
这段代码在我决定将其中一个函数改为在线程中运行之前,运行良好。
当在Python线程中调用numpy函数时,会抛出一个
我寻找了解决方案,但没有找到。能否有人解释一下为什么会发生这种情况,以及如何解决?
IronPython
编写的代码,该代码从C#
应用程序中调用。这段代码在我决定将其中一个函数改为在线程中运行之前,运行良好。
当在Python线程中调用numpy函数时,会抛出一个
InsufficientMemoryException
异常。我寻找了解决方案,但没有找到。能否有人解释一下为什么会发生这种情况,以及如何解决?
我认为只有在有两个使用numpy
的线程时才会出现这种情况。
我运行以下代码:
C#:
_python.functionA(); # _python was created with "Python.CreateEngine()"
_python.functionA(); # twice on purpose
Python:
my_python_script.py
import threading
import time
import numpy
def blah():
print numpy.array([100,100,0])
def functionA():
t = threading.Timer(0,blah)
t.start()
time.sleep(2)
我遇到了这个异常:
Exception in thread Thread-1:
Traceback (most recent call last):
File "c:\Program Files\IronPython 2.7.1\Lib\threading.py", line 552, in _Thread__bootstrap_inner
self.run()
File "c:\Program Files\IronPython 2.7.1\Lib\threading.py", line 756, in run
self.function(*self.args, **self.kwargs)
File "C:\workspace\my_python_script.py", line 113, in blah
print numpy.array([100,100,0])
MemoryError: Exception of type 'System.InsufficientMemoryException' was thrown.
谢谢
更新 13/07/14
即使我只运行一个线程并且通过IronPython解释器运行,而没有使用C#,我仍然会遇到此异常:
C:\>"c:\Program Files\IronPython 2.7.1\ipy.exe"
IronPython 2.7.1 (2.7.0.40) on .NET 4.0.30319.18063
Type "help", "copyright", "credits" or "license" for more information.
>>> execfile(r"c:\workspace\my_python_script.py")
>>> functionA()
>>> Exception in thread Thread-1:
Traceback (most recent call last):
File "c:\Program Files\IronPython 2.7.1\Lib\threading.py", line 552, in _Thread__bootstrap_inner
self.run()
File "c:\Program Files\IronPython 2.7.1\Lib\threading.py", line 756, in run
self.function(*self.args, **self.kwargs)
File "c:\workspace\my_python_script.py", line 6, in blah
print numpy.array([100,100,0])
MemoryError: Exception of type 'System.InsufficientMemoryException' was thrown.