Giter Club home page Giter Club logo

Comments (3)

redfoggg avatar redfoggg commented on May 30, 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)

from conjure.

Olical avatar Olical commented on May 30, 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!

from conjure.

redfoggg avatar redfoggg commented on May 30, 2024

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

from conjure.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.