Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Rename to GetPythonThreadID
  • Loading branch information
gpetrou committed Jan 21, 2021
commit 8e7b7c6d024ff710427987debae8dce8e595f2ae
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +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 GetNativeThreadID and Interrupt methods in PythonEngine
- Add GetPythonThreadID and Interrupt methods in PythonEngine

### Changed
- Drop support for Python 2, 3.4, and 3.5
Expand Down
8 changes: 4 additions & 4 deletions src/embed_tests/TestInterrupt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public void Dispose()
public void InterruptTest()
{
int runSimpleStringReturnValue = int.MinValue;
ulong nativeThreadID = ulong.MinValue;
ulong pythonThreadID = ulong.MinValue;
Task.Factory.StartNew(() =>
{
using (Py.GIL())
{
nativeThreadID = PythonEngine.GetNativeThreadID();
pythonThreadID = PythonEngine.GetPythonThreadID();
runSimpleStringReturnValue = PythonEngine.RunSimpleString(@"
import time

Expand All @@ -47,11 +47,11 @@ import time

Thread.Sleep(200);

Assert.AreNotEqual(ulong.MinValue, nativeThreadID);
Assert.AreNotEqual(ulong.MinValue, pythonThreadID);

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

Expand Down
16 changes: 8 additions & 8 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -568,28 +568,28 @@ public static void Exec(string code, IntPtr? globals = null, IntPtr? locals = nu
}

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

/// <summary>
/// Interrupts the execution of a thread.
/// </summary>
/// <param name="nativeThreadID">The native thread ID.</param>
/// <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 nativeThreadID)
public static int Interrupt(ulong pythonThreadID)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return Runtime.PyThreadState_SetAsyncExcLLP64((uint)nativeThreadID, Exceptions.KeyboardInterrupt);
return Runtime.PyThreadState_SetAsyncExcLLP64((uint)pythonThreadID, Exceptions.KeyboardInterrupt);
}

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

/// <summary>
Expand Down