-
Notifications
You must be signed in to change notification settings - Fork 151
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
SharedInterpreters now share MemoryManager and java PyObjects. #421
SharedInterpreters now share MemoryManager and java PyObjects. #421
Conversation
t.join(); | ||
String test2 = list.getAttr("pop", PyCallable.class).callAs(String.class); | ||
String test = list.getAttr("pop", PyCallable.class).callAs(String.class); | ||
if (!test.equals("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.
Best practice is to put the constant string on the left side of the .equals() call. !"test".equals(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.
Fixed
if (!test.equals("test")) { | ||
throw new IllegalStateException("Expecting 'test', not " + test); | ||
} | ||
if (!test2.equals("test2")) { |
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.
Best practice is to put the constant string on the left side of the .equals() call. !"test2".equals(test2)
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.
Fixed
@@ -80,9 +75,9 @@ protected PyPointer(PyObject referrent, MemoryManager memoryManager, | |||
*/ | |||
protected synchronized void dispose() throws JepException { | |||
if (!disposed) { | |||
decref(memoryManager.getThreadState(), pyObject); | |||
disposed = true; |
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 did you put the decref
ahead of disposed = true
? If decref throws an exception then you could keep attempting to dispose it over and over again, which seems kinda bad.
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 didn't consider that, I changed it to set the disosed
before decref.
try (Interpreter interp2 = new SubInterpreter()) { | ||
list.getAttr("append", PyCallable.class).call("test"); | ||
pass[0] = false; | ||
} catch (JepException e) { |
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.
What is the message on the exception that is thrown? It's not clear to me how you made it work for SharedInterpreters but not SubInterpreters.
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.
The message is "Invalid thread access" from line 122 of MemoryManager.
The MemoryManager is keeping track of which interpreters are sharing memory and uses a ThreadLocal to track which threads those interpreters are valid on. When PyPointer or PyObject gets the thread state for any native operation it is now going through the MemoryManager which will return the thread state if there is an interpreter for that MemoryManager on that thread. If there is no interpreter for a memory manager then the "Invalid thread access" is thrown.
The implementation of the MemoryManager and PyPointer do not specifically behave differently for SubInterpreter vs. SharedInterpreter, But during construction the SharedInterpreters wil use a static MemroyManager that is shared and SubInterpreters will construct a new MemoryManager and this difference in construction causes different behavior.
try (Interpreter interp2 = new SubInterpreter()) { | ||
interp2.set("l", list); | ||
pass[0] = false; | ||
} catch (JepException e) { |
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.
What is the message on the exception that is thrown? It's not clear to me how you made it work for SharedInterpreters but not SubInterpreters.
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.
The message is "Invalid thread access" from line 122 of MemoryManager.
This enables all SharedInterpreters to use the same MemoryManager so that a PyPointer from any SharedInterpreter can be used on threads from any other SharedInterpreters. This requires internal changes to MemoryManager and the way it interacts with PyPointer because there is no longer a one-to-one relationship between a tstate and a MemoryManager, instead the same MemoryManager is valid for multiple tstates.
This should introduce more flexibility for developers. For ThreadPools performing the same task in different interpreters being able to reuse PyObjects should make them a more appealing feature.
I am hoping to expand this feature in a future release so that the MainInterpreter can clean up references from the shared MemoryManager, which would remove the requirement to keep an interpreter open by allowing cleanup without another Interpreter.