What's a good way to run funcs in the repl that might take awhile and you want to cancel before they are done? Or you want to evaluate other things while it is going. Create a thread?
Which repl?
I'm currently using Calva
Which I think is nrepl ?
If it's nRepl, you can send an interupt c-c c-b or the cider-interrupt command and it'll interrupt the running evaluation.
Your code ideally should be interruptible cooperatively, but it also works otherwise, just if you are using JDK 20+ you need to follow these instructions:
https://nrepl.org/nrepl/installation.html#jvmti
Because Java disabled the ability to stop a thread non-cooperatively in JDK20+
As for evaluating other things. nRepl queries up eval and evaluates them one at a time. You can put the code you send for eval inside a future but the evaluation return won't hook back onto your editor. Alternatively, if you connect a second repl in a new nRepl session it can evaluate in parallel.
I tried doing interrupt evaluations and saw an error about couldn't stop thread I'll check that link.
The best way to do what you ask is to not do it and do something else instead. If you must interrupt something, you can reliably interrupt a process but not a thread. In a REPL you might subdivide the task, or fire it off in a future after programming it to check a "please stop now" atom periodically.
Thanks, yeah I just now looking into adding a cancel atom
If you want to do it properly, what you do is add checks for (Thread/interrupted) inside your hot loops. Then you can reliably interrupt it with cider-interrupt.
Otherwise, the agent nRepl implemented for JDK20+ is pretty reliable I would say. I think @phill is maybe not familiar with it?
There is a JDK 20? :-)
I was referring to the JVMTI agent that's shipped with nRepl (caveat does not work on windows)
For example, I can interrupt out of: (while true (+ 1 2 3 4 5 6 7 8))
The reason I say proper interruption is to follow (Thread/interrupted), over a custom shared state flag like through an atom, is that if you also want to be able to interrup out of IO operations, you have to use the JVM native mechanism for cooperative interrupt. So I personally suggest learning it and getting used to it directly. future-cancel also uses it internally, so if you want to know how to make code future-cancellable you also should learn this.