1 Star 0 Fork 0

微距离/pythonnet

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
TestInterrupt.cs 3.02 KB
一键复制 编辑 原始数据 按行查看 历史
Victor Nova 提交于 2021-09-23 12:39 +08:00 . made InterruptTest more robust
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using Python.Runtime;
namespace Python.EmbeddingTest
{
public class TestInterrupt
{
PyObject threading;
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
// workaround for assert tlock.locked() warning
threading = Py.Import("threading");
}
[OneTimeTearDown]
public void Dispose()
{
threading.Dispose();
PythonEngine.Shutdown();
}
[Test]
public void PythonThreadIDStable()
{
long pythonThreadID = 0;
long pythonThreadID2 = 0;
var asyncCall = Task.Factory.StartNew(() =>
{
using (Py.GIL())
{
Interlocked.Exchange(ref pythonThreadID, (long)PythonEngine.GetPythonThreadID());
Interlocked.Exchange(ref pythonThreadID2, (long)PythonEngine.GetPythonThreadID());
}
});
var timeout = Stopwatch.StartNew();
IntPtr threadState = PythonEngine.BeginAllowThreads();
while (Interlocked.Read(ref pythonThreadID) == 0 || Interlocked.Read(ref pythonThreadID2) == 0)
{
Assert.Less(timeout.Elapsed, TimeSpan.FromSeconds(5), "thread IDs were not assigned in time");
}
PythonEngine.EndAllowThreads(threadState);
Assert.IsTrue(asyncCall.Wait(TimeSpan.FromSeconds(5)), "Async thread has not finished in time");
Assert.AreEqual(pythonThreadID, pythonThreadID2);
Assert.NotZero(pythonThreadID);
}
[Test]
public void InterruptTest()
{
long pythonThreadID = 0;
var asyncCall = Task.Factory.StartNew(() =>
{
using (Py.GIL())
{
Interlocked.Exchange(ref pythonThreadID, (long)PythonEngine.GetPythonThreadID());
return PythonEngine.RunSimpleString(@"
try:
import time
while True:
time.sleep(0.2)
except KeyboardInterrupt:
pass");
}
});
var timeout = Stopwatch.StartNew();
IntPtr threadState = PythonEngine.BeginAllowThreads();
while (Interlocked.Read(ref pythonThreadID) == 0)
{
Assert.Less(timeout.Elapsed, TimeSpan.FromSeconds(5), "thread ID was not assigned in time");
}
PythonEngine.EndAllowThreads(threadState);
int interruptReturnValue = PythonEngine.Interrupt((ulong)Interlocked.Read(ref pythonThreadID));
Assert.AreEqual(1, interruptReturnValue);
threadState = PythonEngine.BeginAllowThreads();
Assert.IsTrue(asyncCall.Wait(TimeSpan.FromSeconds(5)), "Async thread was not interrupted in time");
PythonEngine.EndAllowThreads(threadState);
Assert.AreEqual(0, asyncCall.Result);
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/MicroDistanceStudio/pythonnet.git
git@gitee.com:MicroDistanceStudio/pythonnet.git
MicroDistanceStudio
pythonnet
pythonnet
master

搜索帮助