Why does lein run hang? - clojure

I have created a Leiningen project for Exercise 2 from here. My code looks like this:
(ns random-quotes.core
(:require [clojure.string :as str])
(:gen-class))
(defn word-count [s]
(frequencies (str/split (first (str/split s #"\n")) #"\s")))
(def quote-url "http://www.braveclojure.com/random-quote")
(def total-word-count (atom {}))
(defn update-word-count []
(future
(swap! total-word-count
(partial merge-with +)
(word-count (slurp quote-url)))))
(defn quote-word-count [n]
(doseq [quote-future (doall (repeatedly n update-word-count))]
#quote-future)
#total-word-count)
(defn -main [n]
(doseq [entry (sort-by val (quote-word-count (bigdec n)))]
(println entry)))
All pretty straightforward. When I run, e.g., (-main 5) in lein repl, it runs, prints, and returns as expected. However, when I try lein run 5 instead, it runs and prints but never exits, so I am forced to use Ctrl+C to get my terminal back.
Any idea why this happens?

Clojure has a thread pool that it keeps running for use by the agents. Because those threads are still alive, the JVM can't tell that you're program is done. It's just sitting there waiting for the agents to exit. You can make them finish by calling (shutdown-agents) at the end of your program as described here. Futures use agents.
clojure.core/future-call calls an agent like this:
(let [f (binding-conveyor-fn f)
fut (.submit clojure.lang.Agent/soloExecutor ^Callable f)]
which actually starts your code running. You would not be the only one to voice some criticism of this, and we all hope a more elegant solution is found.

Related

What's the simplest way to run a task periodically in clojure

I want to execution a function every 10 seconds. How to do it the simplest way? In core.async, there is a timeout function which will at MOST wait that time, and what I want is to wait at LEAST that time.
at-at is my favorite. Example below is copied from the webpage:
(use 'overtone.at-at)
(def my-pool (mk-pool))
(at (+ 1000 (now)) #(println "hello from the past!") my-pool)
If you use core.async in general, chime is great too.
Again, taken from their examples:
(:require [chime :refer [chime-ch]]
[clj-time.core :as t]
[clojure.core.async :as a :refer [<! go-loop]])
(let [chimes (chime-ch [(-> 2 t/secs t/from-now)
(-> 3 t/secs t/from-now)])]
(a/<!! (go-loop []
(when-let [msg (<! chimes)]
(prn "Chiming at:" msg)
(recur)))))

Simple(st) Datomic test program hangs at exit

The following program, when run from an überjar, exits at the end only when using the in-memory Datomic database; when connecting to the Datomic server, it hangs indefinitely rather than exiting the JVM:
(ns myns.example
(:use [datomic.api :only [db q] :as d])
(:gen-class))
;; WORKS: (def uri "datomic:mem://testdb")
(def uri "datomic:free://localhost:4334/testdb2")
(defn -main []
(println 1)
(when (d/create-database uri)
(d/connect uri))
(shutdown-agents)
(println 2))
Run as:
lein uberjar && java -cp target/myns-0.1.0-SNAPSHOT-standalone.jar myns.example
Outputs:
1
2
and hangs. It only hangs if the DB doesn't exist when the program starts.
Anyone know why, or how to fix? This is with both datomic-free-0.8.4020.26 and datomic-free-0.8.3941.
UPDATE -- the above program does actually terminate, but it takes a very long time (> 1 minute). I'd like to know why.
shutdown-agents takes up to one minute to complete (assuming no agents are running an action).
This is due to the way java.util.concurrent cached thread pools work.
Use datomic.api/shutdown
shutdown
function
Usage: (shutdown shutdown-clojure)
Shut down all peer
resources. This method should be called as part of clean shutdown of
a JVM process. Will release all Connections, and, if shutdown-clojure
is true, will release Clojure resources. Programs written in Clojure
can set shutdown-clojure to false if they manage Clojure resources
(e.g. agents) outside of Datomic; programs written in other JVM
languages should typically set shutdown-clojure to true.
Added in Datomic Clojure version 0.8.3861
(ns myns.example
(:require [datomic.api :as d])
(:gen-class))
(def uri "datomic:free://localhost:4334/testdb2")
(defn -main []
(d/create-database uri)
(let [conn (d/connect uri)]
(try
;; do something
(finally (d/shutdown true)))

writing a test to determine if code is being executed in parallel (future)

I have a piece of code which I run in parallel using a future -
(def services [s1 s2])
(defn get-data []
(->>
(map #(future
(->> (p/fetch %)
(p/parse %)))
services)
(map deref)
(apply concat)))
(get-data)
The requirement for the above piece of code is to run two services in parallel. So I want to create a test that determines if the function (get-data) runs code for both the services in parallel or not. How do write such a test?
Use an agent to record the start and stop events into a sequence, then read the sequence to ensure that it contains at least two start events before the first stop event.
(map #(future
(send watcher conj :start)
(let [result (->> (p/fetch %)
(p/parse %))]
(send watcher conj :finish)
result)))
or you can use with-redefs to wrap your fetch and parse functions to avoid mixing test and production code.

Clojure - (read-string String calling function

I've got the following in a clojure file:
(ns helloworld
(:gen-class
:main -main))
(defn hello-world-fn []
(println "Hello World"))
(defn -main [& args]
(eval (read-string "(hello-world-fn)")))
and I'm running it with
lein run helloworld
and I'm getting the following error:
Exception in thread "main" java.lang.RuntimeException: Unable to resolve symbol:
helloworld in this context, compiling:(helloworld.clj:12)
I have a feeling I need to do something with ns-resolve or resolve but I haven't had any success. I've tried the following in the main function:
(let [call-string (read-string "(hello-world-fn)")
func (resolve (symbol (first call-string)))
args (rest call-string)]
(apply func args))
Without success.
Can someone (a) point me in the right direction; and (b) explain precisely what is going on in the Clojure reader when this occurs?
Try to see what the actual namespace is inside your -main.
(defn -main [& args]
(prn *ns*)
(eval (read-string "(hello-world-fn)")))
It outputs #<Namespace user> before bombing out with the exception. This hints that execution of programs with lein run starts out in the user namespace, which obviously does not contain the mapping for your hello-world-fn symbol. You'll need to explicitly qualify it.
(defn -main [& args]
(eval (read-string "(helloworld/hello-world-fn)")))
You can solve your challenge, in a very elegant way, using macros. In fact, you can write a macro that mimics eval.
(defmacro my-eval [s] `~(read-string s))
(my-eval "(hello-world-fn)")); "Hello World"
It works better that eval because the symbol resolution of s occurs in the context that calls my-eval. Thanks to #Matthias Benkard for the clarifications.
You can read about macros and their syntax in http://clojure.org/reader

Creating a Clojure future without it running

I have a basic Clojure script containing:
(def test
(future
(loop []
(println "Running")
(recur))))
However, if I execure the file with:
java -cp clojure-1.3.0.jar clojure.main test.clj
Then the screen fills with "Running". How can I change it so the future runs when I want it?
Note: I realise this will run forever, it's just an example of my problem.
A future that doesn't run immediately is just a function with no arguments.
So:
(defn test []
(println "Running")
(recur))
...Later...
(future (test))