Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 21, 2021

Conversation

gpetrou
Copy link
Contributor

@gpetrou gpetrou commented Dec 26, 2020

What does this implement/fix? Explain your changes.

Fixes #766 by adding an Interrupt method in PythonEngine class.

Checklist

Check all those that are applicable and complete.

  • Make sure to include one or more tests for your change
  • If an enhancement PR, please create docs and at best an example
  • Add yourself to AUTHORS
  • Updated the CHANGELOG

@gpetrou gpetrou force-pushed the Interrupt branch 9 times, most recently from 9b35013 to d075db3 Compare December 26, 2020 16:34
@filmor
Copy link
Member

filmor commented Dec 26, 2020

Copying my comment from #1333:

  1. This requires a test (done)
  2. Strictly, the thread-id attribute is unsigned only for Python >= 3.7 and we still support 3.6
  3. Also, since the original type is long, the "correct" type is (U)IntPtr on everything but Windows (Not quite sure how far we have to go there compatibility-wise, the current implementation could be fine for the usual thread IDs)
  4. Maybe we should just expose the API function directly (i.e. RaiseInPythonThread on Exception objects or so) and make Interrupt use that

Additional comments on the current state: The helper API to get the current thread id should be in the library and should use threading.get_native_id() on Python >=3.8.

@filmor
Copy link
Member

filmor commented Dec 26, 2020

It would also be good if you'd just use normal commits now without force-pushing a single commit over and over, otherwise it becomes very difficult to follow your changes. We can (and will) squash this in the end.

@gpetrou
Copy link
Contributor Author

gpetrou commented Dec 27, 2020

Please note that I am very unfamiliar with the codebase, so I will need more explanations from you. With regards to:
2. I added a version check. Is this what you mean?
3. I added different calls per OS and version. Is this what you mean?
4. I don't understand what that means. Can you elaborate? Is this still needed?

@gpetrou gpetrou force-pushed the Interrupt branch 3 times, most recently from 07b9655 to c3f7c78 Compare December 27, 2020 07:17
if (Runtime.PyVersion >= new Version(3, 8))
{
dynamic threading = Py.Import("threading");
return threading.get_native_id();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should get the GIL, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the user supposed to do that when calling the method, as I did in the test?

Copy link
Contributor Author

@gpetrou gpetrou Dec 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not seem to be working on Ubuntu. Are you sure that this should work? Looking in CPython tests I see that get_ident is used. I don't see any benefit in adding the above lines. Should I just remove them?

@filmor
Copy link
Member

filmor commented Dec 27, 2020

This goes in the right direction :)

Regarding 2.: Yes, that's what I meant.
Regarding 3.: This looks better, but the Windows type is uint and int. .NET's int is always 32bit, and on Windows long is also 32bit on both x86 and x64.

@gpetrou gpetrou force-pushed the Interrupt branch 8 times, most recently from fb52aa3 to 90a08b6 Compare December 31, 2020 09:08
@gpetrou
Copy link
Contributor Author

gpetrou commented Dec 31, 2020

@filmor can this be merged now?

@@ -2143,6 +2143,18 @@ 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_SetAsyncExc37Windows(uint id, IntPtr exc);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need overloads with uint and int? They are basically equivalent at bit level.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added them because of "Strictly, the thread-id attribute is unsigned only for Python >= 3.7 and we still support 3.6" comment above. Do you want me to remove them?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@filmor on the binary side there's no difference. The caller, if necessary, can bitcast. Any thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed the int and long declarations for now.

Comment on lines 583 to 578
public static ulong GetNativeThreadID()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return GetCurrentThreadId();
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return pthread_selfLinux();
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return pthread_selfOSX();
}

throw new InvalidOperationException("Could not retrieve native thread ID.");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this belongs to PythonEngine. From what I read, for the test you might be able to use threading.get_ident() from Python thread module.

Copy link
Member

@filmor filmor Dec 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_ident is a separate thing, the IDs are not compatible with the "remote raise".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@filmor are you sure? Documentation is very bad on this, but here's what I found:

get_ident appears to be returning PyThread_get_thread_ident
tstate->thread_id is also PyThread_get_thread_ident()

PyThreadState_SetAsyncExc finds thread by its tstate->thread_id

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using get_ident seems to work. What would you like me to do? Modify GetNativeThreadID method or remove it completely?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove it would be better.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lostmsu You are right. But then we should still expose threading.get_ident or PyThread_get_thread_ident, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? The user can do what tests do now - e.g. call Py.Import("threading").Invoke("get_ident").

@gpetrou It might make sense to mention threading.get_ident() in the summary of XML doc for the Interrupt method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am getting mixed signals between the two of you :) Who is the BDFL for Python.NET? I added the get_ident usage in GetNativeThreadID for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gpetrou @filmor well, I am voting for removal, as it is one less thing to support with potential multiple meanings, and having nothing to do with Python embedding on itself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CHANGELOG.md Outdated
@@ -10,6 +10,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
### Added

- Ability to instantiate new .NET arrays using `Array[T](dim1, dim2, ...)` syntax
- Add GetNativeThreadID and Interrupt methods in PythonEngine
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetNativeThreadID should be gone now

@gpetrou gpetrou force-pushed the Interrupt branch 2 times, most recently from ce3d6b4 to 9b18f26 Compare January 8, 2021 07:00
@@ -582,28 +573,8 @@ public static void Exec(string code, IntPtr? globals = null, IntPtr? locals = nu
/// <returns>The native thread ID.</returns>
public static ulong GetNativeThreadID()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we keep this, it should be renamed to GetPythonThreadId and doesn't this require the GIL in general?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain why we need GIL here and not in the Interrupt method? GIL is used for both in the tests already.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, you are right, all of the other functions in here require the GIL to be taken explicitly as well. I'm not terribly happy about this, but this should be fine then. The dynamic here should not be required, just use InvokeMethod:

var threading = ...;
return threading.InvokeMethod("get_ident");

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And renaming would still be nice, sorry for the back and forth, I misread the docs.

@gpetrou gpetrou changed the title Add Interrupt method in PythonEngine Add GetPythonThreadID and Interrupt methods in PythonEngine Jan 21, 2021
@gpetrou gpetrou force-pushed the Interrupt branch 2 times, most recently from 6bcda67 to c455ee4 Compare January 21, 2021 09:00
@lostmsu lostmsu merged commit 5fd77b1 into pythonnet:master Jan 21, 2021
lostmsu added a commit to losttech/pythonnet that referenced this pull request Feb 21, 2021
lostmsu added a commit to losttech/pythonnet that referenced this pull request Feb 21, 2021
lostmsu added a commit to losttech/pythonnet that referenced this pull request Feb 21, 2021
lostmsu added a commit to losttech/pythonnet that referenced this pull request Feb 21, 2021
lostmsu added a commit that referenced this pull request Feb 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Interrupting running Python code from another thread
3 participants