Ants is a task execution library for clojure.
Ants is a wrapper for the ExecutorService of the java.util.concurrent package. It doesn't expose all of the functionality of the Executor but it does provide some additional features.
(use karmag.ants.core) (def prc (make-processor)) (add prc #(println "hello world")) (wait-for prc 1 :m)
- Create and setup a processor. (
- Add tasks to be processed. (
- Await termination. (
configure currently accepts two keys.
:maximum-pool-size. keep-alive-time is the time a thread should idle
before terminating and maximum-pool-size is the the maximum thread
(configure prc :maximum-pool-size 10 :keep-alive-time [15 :s])
addadds a single task and returns a future.
add-alladds a seq of tasks and returns a seq of futures.
shutdownprevents further tasks from being added to the processor.
abortwill attempt to stop ongoing tasks as well remove any queued tasks. It returns a seq of Runnables representing the tasks that were not processed.
Ants has two ways to block until processing finishes.
(wait-for prc 5 :minutes)
shutdownbefore starting to wait. This means that no more tasks can be added after
wait-forhas been called.
wait-whilewill block until all tasks have finished processing but doesn't call
wait-forshould generally be considered first as it is more efficient.
wait-whileis required if additional tasks are to be added later.
Ants provide translation of keywords to TimeUnit objects used by for
wait-for. In instances when there is no timeunit given Ants
treats the given value as milliseconds.
;; regular :days TimeUnit/DAYS :hours TimeUnit/HOURS :microseconds TimeUnit/MICROSECONDS :milliseconds TimeUnit/MILLISECONDS :minutes TimeUnit/MINUTES :nanoseconds TimeUnit/NANOSECONDS :seconds TimeUnit/SECONDS ;; abbreviations :h TimeUnit/HOURS :ms TimeUnit/MILLISECONDS :m TimeUnit/MINUTES :ns TimeUnit/NANOSECONDS :s TimeUnit/SECONDS
The optimizer functions attempts to tune a processor by experimentally wiggling something (thread count) back and forth and measuing it's effect.
(start-optimizer prc :interval [1 :m] :writer *out*)
The above code will adjust each minute and print logging information to standard out. The default values are 10 seconds and to not write any logging information.
Generally the optimizer could just be started and kept running. This
would allow it to adjust to any change in the traffic model at
runtime. Alternatively the logging could be enabled to help find an
optimum which in turn could be hard-coded with
The optimizer can be stopped by calling
release-optimizer on the
return value of