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

Add GetPythonThreadID and Interrupt methods in PythonEngine#1337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
lostmsu merged 4 commits intopythonnet:masterfromgpetrou:Interrupt
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletionsAUTHORS.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -66,10 +66,10 @@
- Ville M. Vainio ([@vivainio](https://github.com/vivainio))
- Virgil Dupras ([@hsoft](https://github.com/hsoft))
- Wenguang Yang ([@yagweb](https://github.com/yagweb))
- William Sardar ([@williamsardar])(https://github.com/williamsardar)
- William Sardar ([@williamsardar](https://github.com/williamsardar))
- Xavier Dupré ([@sdpython](https://github.com/sdpython))
- Zane Purvis ([@zanedp](https://github.com/zanedp))
- ([@amos402]https://github.com/amos402)
- ([@amos402](https://github.com/amos402))
- ([@bltribble](https://github.com/bltribble))
- ([@civilx64](https://github.com/civilx64))
- ([@GSPP](https://github.com/GSPP))
Expand All@@ -82,3 +82,4 @@
- ([@testrunner123](https://github.com/testrunner123))
- ([@DanBarzilian](https://github.com/DanBarzilian))
- ([@alxnull](https://github.com/alxnull))
- ([@gpetrou](https://github.com/gpetrou))
1 change: 1 addition & 0 deletionsCHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,6 +11,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].

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

### Changed
- Drop support for Python 2, 3.4, and 3.5
Expand Down
63 changes: 63 additions & 0 deletionssrc/embed_tests/TestInterrupt.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@

using System;
using System.Threading;
using System.Threading.Tasks;

using NUnit.Framework;

using Python.Runtime;

namespace Python.EmbeddingTest
{
public class TestInterrupt
{
private IntPtr _threadState;

[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
_threadState = PythonEngine.BeginAllowThreads();
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.EndAllowThreads(_threadState);
PythonEngine.Shutdown();
}

[Test]
public void InterruptTest()
{
int runSimpleStringReturnValue = int.MinValue;
ulong pythonThreadID = ulong.MinValue;
Task.Factory.StartNew(() =>
{
using (Py.GIL())
{
pythonThreadID = PythonEngine.GetPythonThreadID();
runSimpleStringReturnValue = PythonEngine.RunSimpleString(@"
import time

while True:
time.sleep(0.2)");
}
});

Thread.Sleep(200);

Assert.AreNotEqual(ulong.MinValue, pythonThreadID);

using (Py.GIL())
{
int interruptReturnValue = PythonEngine.Interrupt(pythonThreadID);
Assert.AreEqual(1, interruptReturnValue);
}

Thread.Sleep(300);

Assert.AreEqual(-1, runSimpleStringReturnValue);
}
}
}
24 changes: 24 additions & 0 deletionssrc/runtime/pythonengine.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -567,6 +567,30 @@ public static void Exec(string code, IntPtr? globals = null, IntPtr? locals = nu
}
}

/// <summary>
/// Gets the Python thread ID.
/// </summary>
/// <returns>The Python thread ID.</returns>
public static ulong GetPythonThreadID()
{
dynamic threading = Py.Import("threading");
return threading.InvokeMethod("get_ident");
}

/// <summary>
/// Interrupts the execution of a thread.
/// </summary>
/// <param name="pythonThreadID">The Python thread ID.</param>
/// <returns>The number of thread states modified; this is normally one, but will be zero if the thread id isn’t found.</returns>
public static int Interrupt(ulong pythonThreadID)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return Runtime.PyThreadState_SetAsyncExcLLP64((uint)pythonThreadID, Exceptions.KeyboardInterrupt);
}

return Runtime.PyThreadState_SetAsyncExcLP64(pythonThreadID, Exceptions.KeyboardInterrupt);
}

/// <summary>
/// RunString Method. Function has been deprecated and will be removed.
Expand Down
6 changes: 6 additions & 0 deletionssrc/runtime/runtime.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2143,6 +2143,12 @@ internal static void Py_CLEAR(ref IntPtr ob)
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern int Py_AddPendingCall(IntPtr func, IntPtr arg);

[DllImport(_PythonDll, EntryPoint = "PyThreadState_SetAsyncExc", CallingConvention = CallingConvention.Cdecl)]
internal static extern int PyThreadState_SetAsyncExcLLP64(uint id, IntPtr exc);

[DllImport(_PythonDll, EntryPoint = "PyThreadState_SetAsyncExc", CallingConvention = CallingConvention.Cdecl)]
internal static extern int PyThreadState_SetAsyncExcLP64(ulong id, IntPtr exc);

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern int Py_MakePendingCalls();

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp