Skip to content
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

Clojure REPL in async environments does not eval output #572

Open
redfoggg opened this issue May 4, 2024 · 3 comments
Open

Clojure REPL in async environments does not eval output #572

redfoggg opened this issue May 4, 2024 · 3 comments

Comments

@redfoggg
Copy link

redfoggg commented May 4, 2024

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
Copy link
Author

redfoggg commented May 4, 2024

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
Copy link
Owner

Olical commented May 6, 2024

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants