Permalink
Browse files

remove default pool nonsense and add convenience fn `after`.

  • Loading branch information...
1 parent 85f6465 commit 6ee61cdf7a1707a920e05c335c849f250c74a8ab @samaaron samaaron committed Mar 27, 2012
Showing with 43 additions and 53 deletions.
  1. +14 −25 README.md
  2. +1 −1 project.clj
  3. +28 −27 src/overtone/at_at.clj
View
@@ -37,50 +37,39 @@ First pull in the lib:
(use 'overtone.at-at)
-Next, schedule the function of your dreams. Here we schedule the function to execute in 1000 ms from now (i.e. 1 second):
-
- (at (+ 1000 (now)) #(println "hello from the past!"))
-
-You can also schedule functions to occur periodically. Here we schedule the function to execute every second:
+`at-at` uses `ScheduledThreadPoolExecutor`s behind the scenes which use a thread pool to run the scheduled tasks. You create a pool, specifying a number of threads with `mk-pool`. For example, here we create a pool with 5 threads:
- (every 1000 #(println "I am cool!"))
-
-This can easily be stopped with `cancel` or its synonym `stop`:
-
- (stop *1)
+ (def my-pool (mk-pool 5))
-Finally, it's also possible to start a periodic repeating fn with an inital delay:
+Next, schedule the function of your dreams. Here we schedule the function to execute in 1000 ms from now (i.e. 1 second):
- (every 1000 #(println "I am cool!") 2000)
+ (at (+ 1000 (now)) my-pool #(println "hello from the past!"))
+Another way of achieving the same result is to use `after` which takes a delaty time in ms from now:
-### Pools
+ (after 1000 my-pool #(println "hello from the past!"))
-`at-at` uses `ScheduledThreadPoolExecutor`s behind the scenes which use a thread pool to run the scheduled tasks. There is a default pool which is created for you with `(+ cpu-count 2)` threads. However, you may create your own and run them separately.
+You can also schedule functions to occur periodically. Here we schedule the function to execute every second:
-For example, here we create a pool with 5 threads:
+ (every 1000 my-pool #(println "I am cool!"))
- (def my-pool (mk-pool 5))
+This returns a shceduled-fn which may easily be stopped with `cancel` or its synonym `stop`:
-We can then use this pool to run our own periodic
+ (stop *1)
- (every 1000 #(println "I am super cool!") my-pool)
+Finally, it's also possible to start a periodic repeating fn with an inital delay:
-And then throw in our old friend running on the default pool:
+ (every 1000 my-pool #(println "I am cool!") 2000)
- (every 1000 #(println "I used to be cool until you made your own pool!"))
+### Resetting a pool.
When necessary it's possible to stop and reset a given pool:
(stop-and-reset-pool! my-pool)
-We can also stop and reset the default pool:
-
- (stop-and-reset-pool!)
-
### Install
-Fetch at-at from github: https://github.com/overtone/at-at or pull from clojars: `[overtone/at-at "0.0.1"]`
+Fetch at-at from github: https://github.com/overtone/at-at or pull from clojars: `[overtone/at-at "X.Y.Z"]`
### History
View
@@ -1,3 +1,3 @@
-(defproject overtone/at-at "0.2.1"
+(defproject overtone/at-at "0.3.0-SNAPSHOT"
:description "Ahead-of-time function scheduler"
:dependencies [[clojure "1.3.0"]])
View
@@ -18,17 +18,12 @@
([num-threads]
(atom (sched-thread-pool num-threads))))
-(defonce default-pool* (mk-pool))
-
(defn every
- "Calls fun every ms-period, and takes an optional initial-delay for the first
- call in ms. Default pool is used if none explicity specified. Returns a
- scheduled-fn which may be cancelled with cancel"
- ([ms-period fun] (every ms-period fun 0))
- ([ms-period fun initial-delay-or-pool] (if (number? initial-delay-or-pool)
- (every ms-period fun initial-delay-or-pool default-pool*)
- (every ms-period fun 0 initial-delay-or-pool)))
- ([ms-period fun initial-delay pool]
+ "Calls fun every ms-period, and takes an optional initial-delay for
+ the first call in ms. Returns a scheduled-fn which may be cancelled
+ with cancel"
+ ([pool ms-period fun] (every pool ms-period fun 0))
+ ([pool ms-period fun initial-delay]
(let [initial-delay (long initial-delay)
ms-period (long ms-period)]
(.scheduleAtFixedRate @pool fun initial-delay ms-period TimeUnit/MILLISECONDS))))
@@ -44,21 +39,15 @@
new-pool))
(defn stop-and-reset-pool!
- "Shuts down a given pool (passed in as an atom) either immediately or not
- depending on whether the optional shutdown-immediately? param is used. The
- pool is then reset to a fresh new pool preserving the original size. If called
- with no params, the default pool is used.
+ "Shuts down a given pool (passed in as an atom) either immediately
+ or not depending on whether the optional shutdown-immediately? param
+ is used. The pool is then reset to a fresh new pool preserving the
+ original size. If called with no params, the default pool is used.
Example usage:
- (stop-and-reset-pool!) ;=> default pool is reset gracefullly
- (stop-and-reset-pool! true) ;=> default pool is reset immediately
- (stop-and-reset-pool! pool) ;=> pool is reset gracefully
+ (stop-and-reset-pool! pool) ;=> pool is reset gracefullly
(stop-and-reset-pool! pool true) ;=> pool is reset immediately"
- ([] (stop-and-reset-pool! default-pool* false))
- ([pool-or-bool] (if (or (= true pool-or-bool)
- (= false pool-or-bool))
- (stop-and-reset-pool! default-pool* pool-or-bool)
- (stop-and-reset-pool! pool-or-bool false)))
+ ([pool] (stop-and-reset-pool! pool false))
([pool shutdown-immediately?]
(swap! pool stop-and-reset-pool shutdown-immediately?)))
@@ -77,14 +66,26 @@
(defn at
"Schedules fun to be executed at ms-time (in milliseconds). Executes
- immediately if ms-time is in the past. Default pool is used if none
- explicitly. Use (now) to get the current time in ms.
+ immediately if ms-time is in the past. Use (now) to get the current
+ time in ms.
Example usage:
- (at (+ 1000 (now)) #(println \"hello from the past\")) ;=> prints 1s from now"
- ([ms-time fun] (at ms-time fun default-pool*))
- ([ms-time fun pool]
+ (at (+ 1000 (now)) #(println \"hello from the past\")) ;=> prints 1s
+ ; from now"
+ ([pool ms-time fun]
(let [delay-time (- ms-time (now))]
(if (<= delay-time 0)
(.execute @pool fun)
(.schedule @pool fun (long delay-time) TimeUnit/MILLISECONDS)))))
+
+(defn after
+ "Schedules fun to be executed after delay-ms (in milliseconds) from
+ now. Executes immediately if ms-time is 0 or negative.
+
+ Example usage:
+ (after 1000 #(println \"hello from the past\")) ;=> prints 1s
+ ; from now"
+ ([pool delay-ms fun]
+ (if (<= delay-ms 0)
+ (.execute @pool fun)
+ (.schedule @pool fun (long delay-ms) TimeUnit/MILLISECONDS))))

0 comments on commit 6ee61cd

Please sign in to comment.