c# – IronPython在线程中使用numpy时抛出InsufficientMemoryException异常

c# – IronPython在线程中使用numpy时抛出InsufficientMemoryException异常,第1张

概述我有一些从C#应用程序中调用的Iron Python代码. 这段代码工作正常,直到我决定更改一个函数来运行在一个线程中. 当在python线程中调用numpy函数时,抛出一个InsufficientMemoryException异常. 我搜索了解决方案,但没有找到.有人可以解释为什么会发生这种情况吗? 我认为只有当我有两个使用numpy的线程时,才发生这种情况 我运行这样的代码: C#: _pyt 我有一些从C#应用程序中调用的Iron Python代码.
这段代码工作正常,直到我决定更改一个函数来运行在一个线程中.
当在python线程中调用numpy函数时,抛出一个InsufficIEntMemoryException异常.
我搜索了解决方案,但没有找到.有人可以解释为什么会发生这种情况吗?

我认为只有当我有两个使用numpy的线程时,才发生这种情况

我运行这样的代码:

C#:

_python.functionA(); # _python was created with "Python.CreateEngine()"_python.functionA(); # twice on purpose

Python:
my_python_script.py

import threadingimport timeimport numpydef 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,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.18063Type "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",**self.kwargs)  file "c:\workspace\my_python_script.py",line 6,0])MemoryError: Exception of type 'system.insufficIEntMemoryException' was thrown.
解决方法 我相信numpy不是线程安全的.我有一个类似的问题,使用np.asarray()与线程会使我的程序崩溃.似乎numpy的数组函数构建数组的方式不是线程安全的.我发现的方法是使用np.fromiter()来代替.显然,它是线程安全的.它稍微慢些,这使得如果它不使用线程,但它是有效的.尝试将数据放入列表(或其他可迭代数据结构)中,并使用np.fromiter()将其转换为numpy数组.

此外,只是这样你知道,它实际上运行在我的电脑上,所以它可能只是你没有足够的内存来处理线程(或至少不使用numpy时).

总结

以上是内存溢出为你收集整理的c# – IronPython在线程中使用numpy时抛出InsufficientMemoryException异常全部内容,希望文章能够帮你解决c# – IronPython在线程中使用numpy时抛出InsufficientMemoryException异常所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1258552.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存