Fork me on GitHub
#nrepl
<
2019-10-26
>
pez16:10:48

Can I interrupt an “evaluation”, resulting from code like this:

(go
    (while true
      (println "hello")
      (Thread/sleep 1000)))

dominicm18:10:35

I don't think so, it's in another thread now.

thheller19:10:01

@pez there is no way to interrupt that no. it would also occupy an entire thread from the thread pool so this must be avoided at all cost 😛

dominicm19:10:42

I wonder if you can poke at that thread pool,

thheller19:10:13

its a var in some namespace

pez19:10:50

How about if it is in ClojureScript? I’m just curious.

thheller19:10:07

CLJS doesn't have Thread/sleep?

pez19:10:49

No, but it has similar constructs, no? So you can evaluate something and get “done” back and it would keep running.

thheller19:10:59

but no you can't interrupt an infinite loop in JS. the browser will kill it eventually together with your page though 😉

thheller19:10:19

no, you can't block in JS

dominicm19:10:50

There's no sleep function at least

thheller19:10:52

if you just keep doing work nothing else will ever happen and your browser/node will lock up

dominicm19:10:10

There are blocking functions (e.g. Nodejs)

thheller19:10:46

ok yes. there are ways to block, but it is a terrible idea to do that 😛

thheller19:10:16

its ok if that is the only thing your program is supposed to be doing anyways

pez19:10:14

So in a ClojureScript REPL, using nREPL, is it possible to interrupt this?

((fn foo []
   (println "hello")
   (js/setTimeout foo 1000)))

pez19:10:45

Or does that fall under this? > no you can’t interrupt an infinite loop in JS

thheller19:10:06

well it will have returned a number

thheller19:10:17

you can use that number to call (js/clearTimeout num)

pez19:10:09

Yeah, but that’s cheating. 😃

thheller19:10:10

but that only works if you hit it before the first repeat 😉

thheller19:10:54

but no that will forever keep doing its thing. no way to stop it. at least it will allow the browser to do other stuff while waiting for the timeout 😛

pez19:10:56

Indeed. It keeps printing here.

thheller19:10:32

you can override what setTimeout does but that gets into really messy territory 😛

pez19:10:51

At least in a browser app I can reload the page.

pez19:10:24

I’m asking this because we’re implementing the interrupt op in Calva.

dominicm19:10:50

Seems like there'd be some value in a debugging api provided by browsers that had these registries available.

thheller19:10:59

interrupt doesn't even work reliably in clojure/JVM

👍 4
pez19:10:04

And we can now stop trying to figure out how these constructs would be interrupted. 😃