-
Notifications
You must be signed in to change notification settings - Fork 256
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
PoC of passing Python Lambdas as Java lambdas #515
Conversation
I expect it to work exactly the same, with the additional benefit of avoiding issues with gc, as the user is more likely to hold on to a reference to the function than with a lambda.
I'd need to check but it might be that it can't use the registered Object class that we use in reflect to be able to use base methods/attributes.
Right that'll be user responsability imho, as we can't know (afaik) if java still holds a reference to it, maybe we could introspect if the proxy object is still referenced by java, but that would need to be something we run regularly? maybe we could tie it to python's gc though. (https://docs.python.org/3/library/gc.html#gc.callbacks my idea would be to save the "holds" in a persistent list, and to run through them in the gc callback to remove them from the list if java doesn't reference their proxy anymore, i can give it a try later if you agree that's sensible).
happy to hear that :) |
Yes, I had a think about this point. At the Java end, we would need a HashMap<Long,WeakReference>, along with a corresponding ReferenceQueue. This could be a static method in NativeInvocationHandler. Every instance of NativeInvocationHandler would be added to this map. At some periodic time (perhaps initiated by Python GC), we would need to walk the values and see which items had been "enqueued" (i.e. Java GCd). We could then delete the corresponding Python object. GC challenges aside or not, I don't think that #515 should delay a 1.3.0 release of Jnius. |
This reverts commit 6781b12.
I'm beginning to think that we should merge this, and deal with the wider GC stuff separately (i.e. currently its "user responsibility" to maintain their Lambda). |
Sorry for not coming back to this before, i now see no reason to wait more to merge this, so it can be part of next release. |
Super, thanks for merging! |
This is a first implementation of passing lambdas to Java.
It works based on several premises:
There is one unit test provided, which demonstrates the following:
in Python as:
Areas for improvement:
Finally, I wanted to add: it only took a few hours to get this working - a true testament to the flexibility/extensibility of jnius!