Leiningen uberjar“空”运行时间

时间:2011-08-24 09:45:22

标签: clojure

用leiningen创建一个uberjar,然后用java -jar foo-uberjar.jar运行jar,程序运行正常,最后一行代码执行得非常快,但是程序在关闭之前会挂起大约一分钟。造成这种情况的原因是什么?

(ns redditwallpaper.core
  (:gen-class)
  (:require [clojure.contrib.duck-streams :as duck]
            [clojure.java [io :as io] [shell :as shell]]
            [clojure.string :as string]
            [clojure.contrib.json :as json])
  (:import (java.util Random)))

(defn filename [uri]
  (last (string/split uri #"/")))
(defn over_18 [x]
  (:over_18 x))
(defn posts [x]
  (map :data (get-in x [:data :children])))
(defn plain-image [x]
  (re-find #"(.jpg|.png)$" (:url x)))
(def url "http://www.reddit.com/r/wallpaper.json")
(def wallpaperjson (json/read-json (slurp url)))
(defn copy [uri fname]
  (with-open [in (io/input-stream uri)
              out (io/output-stream fname)]
    (.write out (duck/to-byte-array in))))
(defn randrange [n]
  (.nextInt (Random.) n))
(defn randitem [xs]
  (let [n (count xs)]
    (nth xs (randrange n))))
(defn set-background [file]
  (shell/sh "feh" "--bg-scale" file))
(defn -main [& args]
  (let [posts (posts wallpaperjson)
        safe (filter (complement over_18) posts)
        images (filter plain-image safe)
        image (:url (randitem images))
        fname (filename image)]
    (do
      (println (format "Downloading '%s'" image))
      (copy image fname)
      (println (format "Setting background image to '%s'" fname))
      (set-background fname)
      (println "Done"))))

java -jar redditwallpaper-0.0.1-standalone.jar 2.71s user 0.34s system 4% cpu 1:03.15 total

2 个答案:

答案 0 :(得分:2)

尝试添加,

(shutdown-agents)

当你准备退出时。

答案 1 :(得分:1)

我在bash / clojure interop的实验中注意到,出于几个原因,更好的方法是简单地使用lein-exec插件。

首先,它对我来说效果很好。

我还从bash运行了几个时间命令来感受我的原始想法之间的区别 - 一方面基本上是为脚本制作lein模板,另一方面是lein-exec。< / p>

一方面,我的实验。我还没有制作lein模板,但想法是:

(ns closcrite.core
  (:require [clojure.java.shell :as shell])
  (:gen-class))

(defn -main
  "I don't do a whole lot."
  [& args]
  (let [shell-output (apply shell/sh args)]
    (println (shell-output :out))
    (shutdown-agents)))

(shutdown-agents) sexpr只是因为我遇到了这个问题。 没有(shutdown-agents)

lein compile && lein uberjar 
  && time java -jar target/closcrite-0.1.0-SNAPSHOT-standalone.jar "ls" "-ahl"
Compiling closcrite.core
Compilation succeeded.
All namespaces already :aot compiled.
Created /home/j/Development/closcrite/target/closcrite-0.1.0-SNAPSHOT.jar
Including closcrite-0.1.0-SNAPSHOT.jar
Including clojure-1.4.0.jar
Created /home/j/Development/closcrite/target/closcrite-0.1.0-SNAPSHOT-standalone.jar
total 36K
drwxrwxr-x  6 j j 4.0K Aug 18 18:14 .
drwxrwxr-x 26 j j 4.0K Aug 18 17:55 ..
drwxrwxr-x  2 j j 4.0K Aug 18 17:55 doc
-rw-rw-r--  1 j j   99 Aug 18 17:55 .gitignore
-rw-rw-r--  1 j j  325 Aug 18 18:14 project.clj
-rw-rw-r--  1 j j  199 Aug 18 17:55 README.md
drwxrwxr-x  3 j j 4.0K Aug 18 17:55 src
drwxrwxr-x  4 j j 4.0K Aug 18 18:02 target
drwxrwxr-x  3 j j 4.0K Aug 18 17:55 test


real    1m2.585s
user    0m4.188s
sys     0m0.120s

使用(shutdown-agents)

lein compile && lein uberjar 
    && time java -jar target/closcrite-0.1.0-SNAPSHOT-standalone.jar "ls" "-ahl"                                                                
Compiling closcrite.core
Compilation succeeded.
All namespaces already :aot compiled.
Created /home/j/Development/closcrite/target/closcrite-0.1.0-SNAPSHOT.jar
Including closcrite-0.1.0-SNAPSHOT.jar
Including clojure-1.4.0.jar
Created /home/j/Development/closcrite/target/closcrite-0.1.0-SNAPSHOT-standalone.jar
total 36K
drwxrwxr-x  6 j j 4.0K Aug 18 18:14 .
drwxrwxr-x 26 j j 4.0K Aug 18 17:55 ..
drwxrwxr-x  2 j j 4.0K Aug 18 17:55 doc
-rw-rw-r--  1 j j   99 Aug 18 17:55 .gitignore
-rw-rw-r--  1 j j  325 Aug 18 18:14 project.clj
-rw-rw-r--  1 j j  199 Aug 18 17:55 README.md
drwxrwxr-x  3 j j 4.0K Aug 18 17:55 src
drwxrwxr-x  4 j j 4.0K Aug 18 18:02 target
drwxrwxr-x  3 j j 4.0K Aug 18 17:55 test


real    0m2.817s
user    0m3.796s
sys     0m0.084s

另一方面,lein-exec:

      time lein exec -e "(ns blah 
                             (:require [clojure.java.shell :as shell])) 
                         (print ((shell/sh \"ls\" \"-ahl\") :out))"
    total 36K
    drwxrwxr-x  6 j j 4.0K Aug 18 18:14 .
    drwxrwxr-x 26 j j 4.0K Aug 18 17:55 ..
    drwxrwxr-x  2 j j 4.0K Aug 18 17:55 doc
    -rw-rw-r--  1 j j   99 Aug 18 17:55 .gitignore
    -rw-rw-r--  1 j j  325 Aug 18 18:14 project.clj
    -rw-rw-r--  1 j j  199 Aug 18 17:55 README.md
    drwxrwxr-x  3 j j 4.0K Aug 18 17:55 src
    drwxrwxr-x  4 j j 4.0K Aug 18 18:02 target
    drwxrwxr-x  3 j j 4.0K Aug 18 17:55 test

    real    0m1.371s
    user    0m2.136s
    sys     0m0.120s

当然time并不是所有的分析结果,但它是速度的一个很好的指示。对于用户来说,内联lein-exec shell脚本感觉几乎是我用来模拟最小leiningen shell脚本模板执行时间的代码的2倍。