Olical / conjure

Interactive evaluation for Neovim (Clojure, Fennel, Janet, Racket, Hy, MIT Scheme, Guile, Python and more!)
https://conjure.oli.me.uk
The Unlicense
1.79k stars 110 forks source link

Clojure REPL in async environments does not eval output #572

Open redfoggg opened 6 months ago

redfoggg commented 6 months ago

image

When trying to print things or get values from an async implementation usually the thing you wanna spill out will not show unless is the last action of a function.

redfoggg commented 6 months ago

Since is not a private code I will submit it here to fast testing with the actual environment, it's a tutorial code.

(ns hospital.aula6
  (:use [clojure pprint])
  (:require
   [hospital.model :as h.model]))

(defn cabe-na-fila?
  [fila]
  (-> fila
      count
      (< 5)))

(defn chega-em
  [fila pessoa]
  (if (cabe-na-fila? fila)
    (conj fila pessoa)
    (throw (ex-info "Fila já está cheia" {:tentando-adiconar pessoa}))))

(defn chega-em! [hospital pessoa]
  (let [fila (get hospital :espera)]
    (alter fila chega-em pessoa)))

(defn simula-um-dia
  []
  (let [hospital {:espera (ref h.model/empty-queue)
                  :laboratorio1 (ref h.model/empty-queue)
                  :laboratorio2 (ref h.model/empty-queue)
                  :laboratorio3 (ref h.model/empty-queue)}]
    (dosync
     (chega-em! hospital "guilherme")
     (chega-em! hospital "ana")
     (chega-em! hospital "paulo")
     (chega-em! hospital "paloma")
     (chega-em! hospital "maria")
     (chega-em! hospital "david"))
    (pprint hospital)))

(simula-um-dia)

(defn async-chega-em! [hospital pessoa]
  (future
    (Thread/sleep (rand 5000))
    (dosync
     (pprint "Tentando adicionar pessoa" pessoa)
     (chega-em! hospital pessoa))))

(defn simula-um-dia-async
  []
  (let [hospital {:espera (ref h.model/empty-queue)
                  :laboratorio1 (ref h.model/empty-queue)
                  :laboratorio2 (ref h.model/empty-queue)
                  :laboratorio3 (ref h.model/empty-queue)}]
    (async-chega-em! hospital 15)
    (pprint hospital)))

(simula-um-dia-async)

(def empty-queue clojure.lang.PersistentQueue/EMPTY)

Olical commented 6 months ago

So you are expecting to see "Tentando adicionar pessoa" in the log and you are not? Do you see the output in you Clojure nREPL window instead? Because you're print is in a different thread to the nREPL thread it means Conjure doesn't get told about the output, so in that case it normally goes to the original nREPL you are connected to.

Running :ConjureOutSubscribe inside your editor may fix that problem and redirect ALL console output into Conjure.

If you are expecting to see values in :laboratorio1 etc though, you will not when they're pprint-ed at the end of simula-um-dia-async. When you call pprint the code inside async-chega-em! has not run yet. So you will need to store the references to the queues or the hospital map somewhere and pprint it later when the future has completed.

That part is just how Clojure (and all async programming) works and isn't something I can change I'm afraid. So! If your question is about the println not showing up, I hope the command I mentioned helps, if it's about the async code not running in time, I'm afraid that's just a normal programming thing and you'll need to make your code check periodically or wait (using https://clojuredocs.org/clojure.core/deref etc) until the async code is complete before printing it.

I hope this helps!

redfoggg commented 6 months ago

I'm trying to see the "Tentando adicionar pessoa". I did try to use the command you mentioned, nothing different happened, the output still get's "ignored" like you said it's running in another thread. Another thing is, looking at the nREPL started by :Lein I cannot see there too, even after waiting some time, it's not a game ending behavior but in IntelliJ if you wait for some time the output gets there, so I think it should be doable. image image