我知道IronPython是.NET平台上的Python实现,就像IronRuby是Ruby的实现一样,F#或多或少都是OCaml.
我无法理解的是这些语言是否更接近他们的"祖先",或者更接近C#这样的速度.例如,IronPython是否以某种方式"编译"到C#使用的相同字节码,因此运行速度一样快?
我需要一些帮助来找到我遇到的内存泄漏的解决方案.我有一个C#应用程序(.NET v3.5),允许用户运行IronPython脚本进行测试.脚本可以加载Python标准库中的不同模块(包含在IronPython二进制文件中).但是,当脚本完成时,分配给导入模块的内存不会被垃圾回收.循环使用一个脚本的多次运行(完成压力测试)会导致系统在长期使用期间耗尽内存.
这是我正在做的简化版本.
脚本类主要功能:
public void Run()
{
// set up iron python runtime engine
this.engine = Python.CreateEngine(pyOpts);
this.runtime = this.engine.Runtime;
this.scope = this.engine.CreateScope();
// compile from file
PythonCompilerOptions pco = (PythonCompilerOptions)this.engine.GetCompilerOptions();
pco.Module &= ~ModuleOptions.Optimized;
this.script = this.engine.CreateScriptSourceFromFile(this.path).Compile(pco);
// run script
this.script.Execute(this.scope);
// shutdown runtime (run atexit functions that exist)
this.runtime.Shutdown();
}
Run Code Online (Sandbox Code Playgroud)
加载随机模块的示例'test.py'脚本(添加~1500 KB的内存):
import random
print "Random number: %i" % random.randint(1,10)
Run Code Online (Sandbox Code Playgroud)
一种循环机制,会导致系统内存不足:
while(1)
{
Script s = new Script("test.py");
s.Run();
s.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
我添加了一节以不根据我在此线程中找到的内容优化编译,但内存泄漏发生在任何一种方式.将显式调用添加到s.Dispose()也没有任何区别(如预期的那样).我目前正在使用IronPython 2.0,但我也尝试过升级到IronPython 2.6 RC2而没有任何成功.
当脚本引擎/运行时超出范围时,如何将嵌入式IronPython脚本中导入的模块像普通.NET对象一样进行垃圾收集?
我目前正在寻找一种可能的方法来将我的C++/C#应用程序与我的一些Python脚本集成.在这一点上,IronPython似乎是要走的路.
但是,在继续之前,我想问以下问题:
我特别感兴趣的是使用IronPython来运行诸如Django或Web2py之类的Python Web框架.我非常清楚当前的Python Web框架与它的搭配并不是很好.因此,对IronPython的Web框架支持的未来的任何见解也将非常受欢迎.
是否有一个列出与IronPython一起使用的标准库和第三方模块的地方?如果没有,请在这里告诉我.
以下是我尝试过的一些内容:
在我的C#代码中,我有一个类,它存储了一些我希望传递给List中的python代码的数据.但是,当我尝试在我的python代码中访问该类的属性时,我得到了MissingMemberException.这里有一些示例代码来说明我的意思:
C#:
class Event
{
public int EventId { get; set; }
public string EventName { get; set; }
}
//other processing here...
//this just fills the list with event objects
List<Event> eventList = GetEvents();
//this sets a variable in the ScriptScope
PythonEngine.SetVariable( "events", eventList);
PythonEngine.Execute("eventParser.py");
Run Code Online (Sandbox Code Playgroud)
eventParser.py:
for e in events:
print e.EventId, " / ", e.EventName
Run Code Online (Sandbox Code Playgroud)
该MissingMemberException说"事件中没有名为EVENTID成员"
我已经测试通过其他类型的蟒蛇,包括原始类型,如列表List< int >和List< string >他们很好地工作.
那么我如何访问这些类属性,EventId并EventName在我的python脚本中?
我们可以使用iron python在.NET空间中加载pandas DataFrame吗?如果不是,我正在考虑将pandas df转换为csv文件,然后读入.net空间.
我想System.Array在IronPython中添加(算术)两个大元素元素并将结果存储在第一个数组中,如下所示:
for i in range(0:ArrA.Count) :
arrA.SetValue(i, arrA.GetValue(i) + arrB.GetValue(i));
Run Code Online (Sandbox Code Playgroud)
但是,这似乎很慢.有C背景我想使用指针或迭代器.但是,我不知道如何快速应用IronPython习语.我不能使用Python列表,因为我的对象严格来自System.Array类型.类型是3d浮点数.
什么是紧固件/计算此计算的快速方法?
编辑:
我有以下C#代码,我从C#调用python脚本:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using IronPython.Runtime;
namespace RunPython
{
class Program
{
static void Main(string[] args)
{
ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null);
ScriptRuntime runtime = new ScriptRuntime(setup);
ScriptEngine engine = Python.GetEngine(runtime);
ScriptSource source = engine.CreateScriptSourceFromFile("HelloWorld.py");
ScriptScope scope = engine.CreateScope();
source.Execute(scope);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我无法理解代码的每一行,因为我对C#的经验有限.当我运行它时,如何更改此代码以便将命令行参数传递给我的python脚本?
我试图使用clr.AddReference和clr.AddReferenceToFile来导入程序集,但python(2.7)不断发出此错误:
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
clr.AddReference("UnityEngine")
AttributeError: 'module' object has no attribute 'AddReference'
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我如何解决这个问题.
我正在尝试将python代码集成到我的.net核心应用程序中,以进行数据分析和一些机器学习分类。我怎样才能做到最好?
注意:我不想使用IronPython执行脚本。我已经尝试过了。
ironpython ×10
python ×6
.net ×5
c# ×3
python.net ×3
clr ×1
iterator ×1
memory-leaks ×1
pandas ×1
performance ×1
system.array ×1