-
Notifications
You must be signed in to change notification settings - Fork 752
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
Conversation
9b35013
to
d075db3
Compare
Copying my comment from #1333:
Additional comments on the current state: The helper API to get the current thread id should be in the library and should use |
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. |
Please note that I am very unfamiliar with the codebase, so I will need more explanations from you. With regards to: |
07b9655
to
c3f7c78
Compare
src/runtime/pythonengine.cs
Outdated
if (Runtime.PyVersion >= new Version(3, 8)) | ||
{ | ||
dynamic threading = Py.Import("threading"); | ||
return threading.get_native_id(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
This goes in the right direction :) Regarding 2.: Yes, that's what I meant. |
fb52aa3
to
90a08b6
Compare
@filmor can this be merged now? |
src/runtime/runtime.cs
Outdated
@@ -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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
src/runtime/pythonengine.cs
Outdated
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."); | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@filmor ?
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 |
There was a problem hiding this comment.
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
ce3d6b4
to
9b18f26
Compare
src/runtime/pythonengine.cs
Outdated
@@ -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() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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");
There was a problem hiding this comment.
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.
…ate_SetAsyncExc calls for OS and Python version
…ic methods and assert nativeThreadID value
6bcda67
to
c455ee4
Compare
related to pythonnet#1337
should resolve this failure: https://github.com/pythonnet/pythonnet/pull/1392/checks?check_run_id=1944113649 related to pythonnet#1337
should resolve this failure: https://github.com/pythonnet/pythonnet/pull/1392/checks?check_run_id=1944113649 related to pythonnet#1337
should resolve this failure: https://github.com/pythonnet/pythonnet/pull/1392/checks?check_run_id=1944113649 related to pythonnet#1337
should resolve this failure: https://github.com/pythonnet/pythonnet/pull/1392/checks?check_run_id=1944113649 related to #1337
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.
AUTHORS
CHANGELOG