Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9b35013

Browse files
committed
Add Interrupt method in PythonEngine
1 parent1f40564 commit9b35013

File tree

5 files changed

+99
-2
lines changed

5 files changed

+99
-2
lines changed

‎AUTHORS.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@
6565
- Ville M. Vainio ([@vivainio](https://github.com/vivainio))
6666
- Virgil Dupras ([@hsoft](https://github.com/hsoft))
6767
- Wenguang Yang ([@yagweb](https://github.com/yagweb))
68-
- William Sardar ([@williamsardar])(https://github.com/williamsardar)
68+
- William Sardar ([@williamsardar](https://github.com/williamsardar))
6969
- Xavier Dupré ([@sdpython](https://github.com/sdpython))
7070
- Zane Purvis ([@zanedp](https://github.com/zanedp))
71-
- ([@amos402]https://github.com/amos402)
71+
- ([@amos402](https://github.com/amos402))
7272
- ([@bltribble](https://github.com/bltribble))
7373
- ([@civilx64](https://github.com/civilx64))
7474
- ([@GSPP](https://github.com/GSPP))
@@ -81,3 +81,4 @@
8181
- ([@testrunner123](https://github.com/testrunner123))
8282
- ([@DanBarzilian](https://github.com/DanBarzilian))
8383
- ([@alxnull](https://github.com/alxnull))
84+
- ([@gpetrou](https://github.com/gpetrou))

‎CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
1010
###Added
1111

1212
- Ability to instantiate new .NET arrays using`Array[T](dim1, dim2, ...)` syntax
13+
- Add Interrupt method in PythonEngine
1314

1415
###Changed
1516
- Drop support for Python 2, 3.4, and 3.5

‎src/embed_tests/TestInterrupt.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
usingSystem;
3+
usingSystem.Runtime.InteropServices;
4+
usingSystem.Threading;
5+
usingSystem.Threading.Tasks;
6+
7+
usingNUnit.Framework;
8+
9+
usingPython.Runtime;
10+
11+
namespacePython.EmbeddingTest
12+
{
13+
publicclassTestInterrupt
14+
{
15+
privateIntPtr_threadState;
16+
17+
[DllImport("Kernel32",EntryPoint="GetCurrentThreadId",ExactSpelling=true)]
18+
privatestaticexternuintGetCurrentThreadId();
19+
20+
[DllImport("libc",EntryPoint="pthread_self")]
21+
privatestaticexternIntPtrpthread_selfLinux();
22+
23+
[DllImport("pthread",EntryPoint="pthread_self",CallingConvention=CallingConvention.Cdecl)]
24+
privatestaticexternulongpthread_selfOSX();
25+
26+
[OneTimeSetUp]
27+
publicvoidSetUp()
28+
{
29+
PythonEngine.Initialize();
30+
_threadState=PythonEngine.BeginAllowThreads();
31+
}
32+
33+
[OneTimeTearDown]
34+
publicvoidDispose()
35+
{
36+
PythonEngine.EndAllowThreads(_threadState);
37+
PythonEngine.Shutdown();
38+
}
39+
40+
[Test]
41+
publicvoidInterruptTest()
42+
{
43+
intrunSimpleStringReturnValue=int.MinValue;
44+
ulongnativeThreadId=0;
45+
Task.Factory.StartNew(()=>
46+
{
47+
if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
48+
{
49+
nativeThreadId=GetCurrentThreadId();
50+
}
51+
elseif(RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
52+
{
53+
nativeThreadId=(ulong)pthread_selfLinux();
54+
}
55+
elseif(RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
56+
{
57+
nativeThreadId=pthread_selfOSX();
58+
}
59+
60+
using(Py.GIL())
61+
{
62+
runSimpleStringReturnValue=PythonEngine.RunSimpleString(@"
63+
import time
64+
65+
while True:
66+
time.sleep(0.2)");
67+
}
68+
});
69+
70+
Thread.Sleep(100);
71+
72+
using(Py.GIL())
73+
{
74+
intinterruptReturnValue=PythonEngine.Interrupt(nativeThreadId);
75+
Assert.AreEqual(1,interruptReturnValue);
76+
}
77+
78+
Thread.Sleep(200);
79+
80+
Assert.AreEqual(-1,runSimpleStringReturnValue);
81+
}
82+
}
83+
}

‎src/runtime/pythonengine.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,15 @@ public static void Exec(string code, IntPtr? globals = null, IntPtr? locals = nu
567567
}
568568
}
569569

570+
/// <summary>
571+
/// Interrupts the execution of a thread.
572+
/// </summary>
573+
/// <param name="nativeThreadId">The native thread id.</param>
574+
/// <returns>The number of thread states modified; this is normally one, but will be zero if the thread id isn’t found.</returns>
575+
publicstaticintInterrupt(ulongnativeThreadId)
576+
{
577+
returnRuntime.PyThreadState_SetAsyncExc(nativeThreadId,Exceptions.KeyboardInterrupt);
578+
}
570579

571580
/// <summary>
572581
/// RunString Method. Function has been deprecated and will be removed.

‎src/runtime/runtime.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,6 +2143,9 @@ internal static void Py_CLEAR(ref IntPtr ob)
21432143
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl)]
21442144
internalstaticexternint Py_AddPendingCall(IntPtr func, IntPtr arg);
21452145

2146+
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl)]
2147+
internalstaticexternint PyThreadState_SetAsyncExc(ulong id, IntPtr exc);
2148+
21462149
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl)]
21472150
internalstaticexternint Py_MakePendingCalls();
21482151

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp