gpt4 book ai didi

c# - 为什么从 C# 调用 Python lambda 表达式不是线程安全的?

转载 作者:IT王子 更新时间:2023-10-29 04:15:00 29 4
gpt4 key购买 nike

我在 IronPython 中定义了一个无副作用的(纯)lambda 表达式,并将其分配给一个 C# 委托(delegate)。当从多个线程同时调用委托(delegate)时,我得到类型为 AccessViolationExceptionNullReferenceExceptionFatalEngineExecutionError 的异常。

错误的发生是不确定的,通常需要数百万次迭代才能引发错误,这对我来说就是“竞争条件”。我怎样才能避免它?

只有在使用 x64(x86 不会崩溃)并且在调试器之外运行进程时才会引发异常。测试系统是 Windows 7、.NET Framework 4.0 和 IronPython 2.7.1 上的 Core I7(8 线程)。

这是产生错误的最少代码:

var engine = Python.CreateEngine();

double a = 1.0;
double b = 2.0;

while (true)
{
Func<double, double, double> calculate = engine.Execute("lambda a,b : a+b");

System.Threading.Tasks.Parallel.For(0, 1000, _ =>
{
for (int i = 0; i < 1000; i++) { calculate(a,b); }
});

Console.Write(".");
}

错误信息:

FatalExecutionEngineError was detected

Message: The runtime has encountered a fatal error. The address of the error was at 0xf807829e, on thread 0x3da0. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

更新:即使引擎被声明为线程局部的,它也会在一段时间后崩溃:

var calculate = new ThreadLocal<Func<double, double, double>>(() => Python.CreateEngine().Execute("lambda a,b : a+b"));

最佳答案

这可能是由于 ScriptEngine 中的竞争条件。请注意,ScriptEngine.Execute 返回对 PythonFunction 而不是 Func 的引用(这是由于 C# 的动态行为,您可以将结果视为 Func。我不是 IronPython 专家,但查看 PythonFunction 的源代码,有没有任何迹象表明它是线程安全的。

关于c# - 为什么从 C# 调用 Python lambda 表达式不是线程安全的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8278036/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com