Michal Hojgr
Michal Hojgr

Reputation: 134

When do side effects happen?

I'm building an app with blocking HTTP calls. I'd like to print something before the block and then continue with something after the blocking call is done. My first tought was to do it like this

(print "Processing " item-hash "...")
(query-item item-hash)
(Thread/sleep 10000)
(println "Done")))

Sleep is there just to slow it down even more to be 100% sure it does what it's supposed to.

In my understanding, it should print the message, start query-item, then sleep for 10 seconds and print Done after that. But currently it waits until the entire parent sexpr is done and prints after that (prints the entire line when all blocking calls are done.

Entire main for reference

(defn -main [& echo]
  (map
    (fn [url]
      (let [item-hash (market-listing-url-to-hash-name url)]
        (print "Processing " item-hash "...")
        (query-item item-hash)
        (Thread/sleep 10000)
        (println "Done")))

    (collect-urls 1)))

Thanks

Upvotes: 0

Views: 76

Answers (1)

Michal Hojgr
Michal Hojgr

Reputation: 134

Appearently print doesn't automatically flush the buffer to the screen. Adding (flush) after the first print fixes it. println flushes it itself.

source code: println calls prn function that has (flush) as one of the side effects prn

Upvotes: 1

Related Questions