Hi there. I have a setup with both Clojure and Java files where I'm using Clojure as a wrapper and Java as a lower layer to work with other things. Is there a way, from Java, to print to whatever Clojure's out
is? Thanks in advance.
@rextruong If you are calling a Java component from Clojure, you can just pass *out*
as an argument and then .write
to that
you can use Clojure.*var*("clojure.core", "*out*");
to get the root binding of out
you still need to deref that though if used from java
The root binding is probably not so interesting since it's just a stream to stdout
that's fair, but in order to know a specific binding, you would need to have your handle on a specific thread / context. which is implicit in clojure but it's a weird hoop to jump through if interacting with clojure via java
@noisesmith He is calling Java from Clojure. So you can just pass *out*
as an argument which will pass the dynamically bound value to the Java side
oh - I must have misunderstood the question
actually, if the java code is being called from clojure, resolving *out*
doesn't get the root binding, it gets the dynamic binding
as long as you didn't do something like call the Thread
constructor and lose your binding
nice
Unfortunately I'm not able to pass any additional info from Clojure to Java since I can't touch the method signature.
Though that being said I can just put it as a class property and use it instead :thinking_face:
Will give it a try. Thanks 🙂
Yep seems like a great solution. Thanks a lot 🙂
:)
what is a class property?
public class Foo {
private int bar;
}
bar is a class property. Though my terminology may be a bit wonky.Actually it's a class field.
Is there a way to initialize InheritableThreadLocal
in all the Clojure thread pool? (Such that it will be defined for future
, pmap
, etc.)
@frozenlock doesn't that already work?
user=> (def tl (proxy [java.lang.InheritableThreadLocal] [] (childValue [v] (.clone ^java.util.Map v))))
#'user/tl
user=> (.set tl (java.util.HashMap. {:a 1 :b 2}))
nil
user=> (.get tl)
{:a 1, :b 2}
user=> (.put (.get tl) :b 3)
2
user=> (.get tl)
{:a 1, :b 3}
user=> @(future (.put (.get tl) :b 4) (.get tl))
{:a 1, :b 4}
user=> (.get tl)
{:a 1, :b 3}
Not if the thread used was already initialized/created. In that case (.get tl)
will return nil.
(pmap (fn [_] (.get tl)) (range 10))
can give inconsistent result.
Perhaps there's a way to scrap all existing threads and start fresh, once the InheritableThreadLocal is defined?
Why do you want that instead of, say, an atom?
Long story short: I need an inheritable dynamic value. (IE capable of transparently moving between future
, or pure java threads)
I'm pretty much there... except for existing threads in which the InheritableThreadLocal
isn't defined.
You can set your own thread pool to use
http://clojure.github.io/clojure/clojure.core-api.html#clojure.core/set-agent-send-executor!
And the send-off equivalent (which future uses)
I would consider these hostile in a library, but ok in an app
Ah, it might be what I need. Thank you!
@frozenlock Can I ask you what you are using InheritableThreadLocal for? I recently discovered it and had a use case for it, but I wonder what other people are using this for.
Sure.
It's to accumulate some debug values that can ultimately be used inside UncaughtExceptionHandler
.
cool
(context 1 .... (context 2 .... (context 3... BOOM))) <---- exception handler should be able to see the 3 contexts.
What was yours?
https://github.com/borkdude/sci/issues/416#issuecomment-757021743
Quick question for Vs code and Calva users. How do you stop evaluating when your functions enters an infinite loop?
Tried Ctrl-Alt-C Ctrl-Alt-D, but that does not seem to do the trick. (Should stop evaluations).
@georghagen FYI, there's a #calva channel where you might get answers faster for Calva-specific questions in future.
Ahh great, thanks 😃 Ill go there instead
For anyone reading it here. There's a command in Calva for this. Not at a computer right now, but something like Interrupt running evaluations.
Ah, and that is the command you tried. Could also be that you have lots of printing in that runaway loop. It's an issue we yet have to fix. Interrupt does not stop the queued printing.
:thumbsup: At least I found why it was entering an infinite loop now
Here's what I ended up, in case it could be useful https://gist.github.com/Frozenlock/e0c8b8f81838182fcf36ae14e7dfea5d
thanks for sharing!