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

Commit5fd77b1

Browse files
authored
Add PythonEngine.Interrupt (#1337)
Also added GetPythonThreadID
1 parent0f33f71 commit5fd77b1

File tree

5 files changed

+97
-2
lines changed

5 files changed

+97
-2
lines changed

‎AUTHORS.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@
6666
- Ville M. Vainio ([@vivainio](https://github.com/vivainio))
6767
- Virgil Dupras ([@hsoft](https://github.com/hsoft))
6868
- Wenguang Yang ([@yagweb](https://github.com/yagweb))
69-
- William Sardar ([@williamsardar])(https://github.com/williamsardar)
69+
- William Sardar ([@williamsardar](https://github.com/williamsardar))
7070
- Xavier Dupré ([@sdpython](https://github.com/sdpython))
7171
- Zane Purvis ([@zanedp](https://github.com/zanedp))
72-
- ([@amos402]https://github.com/amos402)
72+
- ([@amos402](https://github.com/amos402))
7373
- ([@bltribble](https://github.com/bltribble))
7474
- ([@civilx64](https://github.com/civilx64))
7575
- ([@GSPP](https://github.com/GSPP))
@@ -82,3 +82,4 @@
8282
- ([@testrunner123](https://github.com/testrunner123))
8383
- ([@DanBarzilian](https://github.com/DanBarzilian))
8484
- ([@alxnull](https://github.com/alxnull))
85+
- ([@gpetrou](https://github.com/gpetrou))

‎CHANGELOG.md

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

1212
- Ability to instantiate new .NET arrays using`Array[T](dim1, dim2, ...)` syntax
1313
- Python operator method will call C# operator method for supported binary and unary operators ([#1324][p1324]).
14+
- Add GetPythonThreadID and Interrupt methods in PythonEngine
1415

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

‎src/embed_tests/TestInterrupt.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
usingSystem;
3+
usingSystem.Threading;
4+
usingSystem.Threading.Tasks;
5+
6+
usingNUnit.Framework;
7+
8+
usingPython.Runtime;
9+
10+
namespacePython.EmbeddingTest
11+
{
12+
publicclassTestInterrupt
13+
{
14+
privateIntPtr_threadState;
15+
16+
[OneTimeSetUp]
17+
publicvoidSetUp()
18+
{
19+
PythonEngine.Initialize();
20+
_threadState=PythonEngine.BeginAllowThreads();
21+
}
22+
23+
[OneTimeTearDown]
24+
publicvoidDispose()
25+
{
26+
PythonEngine.EndAllowThreads(_threadState);
27+
PythonEngine.Shutdown();
28+
}
29+
30+
[Test]
31+
publicvoidInterruptTest()
32+
{
33+
intrunSimpleStringReturnValue=int.MinValue;
34+
ulongpythonThreadID=ulong.MinValue;
35+
Task.Factory.StartNew(()=>
36+
{
37+
using(Py.GIL())
38+
{
39+
pythonThreadID=PythonEngine.GetPythonThreadID();
40+
runSimpleStringReturnValue=PythonEngine.RunSimpleString(@"
41+
import time
42+
43+
while True:
44+
time.sleep(0.2)");
45+
}
46+
});
47+
48+
Thread.Sleep(200);
49+
50+
Assert.AreNotEqual(ulong.MinValue,pythonThreadID);
51+
52+
using(Py.GIL())
53+
{
54+
intinterruptReturnValue=PythonEngine.Interrupt(pythonThreadID);
55+
Assert.AreEqual(1,interruptReturnValue);
56+
}
57+
58+
Thread.Sleep(300);
59+
60+
Assert.AreEqual(-1,runSimpleStringReturnValue);
61+
}
62+
}
63+
}

‎src/runtime/pythonengine.cs

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

570+
/// <summary>
571+
/// Gets the Python thread ID.
572+
/// </summary>
573+
/// <returns>The Python thread ID.</returns>
574+
publicstaticulongGetPythonThreadID()
575+
{
576+
dynamicthreading=Py.Import("threading");
577+
returnthreading.InvokeMethod("get_ident");
578+
}
579+
580+
/// <summary>
581+
/// Interrupts the execution of a thread.
582+
/// </summary>
583+
/// <param name="pythonThreadID">The Python thread ID.</param>
584+
/// <returns>The number of thread states modified; this is normally one, but will be zero if the thread id isn’t found.</returns>
585+
publicstaticintInterrupt(ulongpythonThreadID)
586+
{
587+
if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
588+
{
589+
returnRuntime.PyThreadState_SetAsyncExcLLP64((uint)pythonThreadID,Exceptions.KeyboardInterrupt);
590+
}
591+
592+
returnRuntime.PyThreadState_SetAsyncExcLP64(pythonThreadID,Exceptions.KeyboardInterrupt);
593+
}
570594

571595
/// <summary>
572596
/// RunString Method. Function has been deprecated and will be removed.

‎src/runtime/runtime.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,6 +2143,12 @@ 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, EntryPoint= "PyThreadState_SetAsyncExc", CallingConvention= CallingConvention.Cdecl)]
2147+
internalstaticexternint PyThreadState_SetAsyncExcLLP64(uint id, IntPtr exc);
2148+
2149+
[DllImport(_PythonDll, EntryPoint= "PyThreadState_SetAsyncExc", CallingConvention= CallingConvention.Cdecl)]
2150+
internalstaticexternint PyThreadState_SetAsyncExcLP64(ulong id, IntPtr exc);
2151+
21462152
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl)]
21472153
internalstaticexternint Py_MakePendingCalls();
21482154

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp