Skip to content

Instantly share code, notes, and snippets.

@pithyless
Forked from pesterhazy/background.clj
Created April 1, 2018 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pithyless/c854f77019e25d17c0c4ff3473280c48 to your computer and use it in GitHub Desktop.
Save pithyless/c854f77019e25d17c0c4ff3473280c48 to your computer and use it in GitHub Desktop.
(ns background
(:require [clojure.tools.logging :as log])
(:import [java.util.concurrent]))
(defonce !executor (delay (java.util.concurrent.Executors/newCachedThreadPool)))
(defn background
"Calls the fn passed in a thread from a thread pool, returning immediately.
Unlike future, background does not swallow exceptions."
[f]
(.execute @!executor (fn [] (try
(f)
(catch Exception e
(log/error "Uncaught exception in background thread:" e))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment