Idiomatic way to execute a task in a thread pool (clojure internal)?

I have tasks that I need to complete in the background. They do not provide interesting value, their side effects are their meaning. These effects will be seen elsewhere later.

I couldn't find any function / macro in clojure that just takes a function and executes it on a background thread. the future may be a coincidence, but I'm a little unsure as I will never smooth it out. This is not a problem in itself, but I don't really care if there is a chance the future will be canceled if the future object is garbage collected and the future hasn't started yet?

I am doing something like this:

(doseq [task-fn task-fns]
  (future (task-fn))

      

+3


source to share


2 answers


Clojure functions implement Runnable, so you can turn them into threads that will terminate in the background. See this answer here - fooobar.com/questions/134675 / ...



+2


source


There is nothing wrong with using future

thread allocation for side effects. It really doesn't matter if you want to get the result or not. And no, the thread future

will not be canceled because of the gargabe collection.

As mentioned earlier, you can use Java's thread execution engines absolutely directly. It's really only a matter of style you choose. I usually use it future

personally.



If you are using core.async

, use thread

or go

is considered more appropriate (as I consider a combination of practical and stylistic reasons, since it returns a channel). But I guess that is not your case here.

+3


source







All Articles