我正在使用Clojure应用程序从Web API访问数据。我将要发出大量请求,并且许多请求将导致发出更多请求,因此我希望将请求URL保留在后续下载之间留出60秒的队列中。
关注this blog post我把它放在一起:
(def queue-delay (* 1000 60)) ; one minute
(defn offer!
[q x]
(.offerLast q x)
q)
(defn take!
[q]
(.takeFirst q))
(def my-queue (java.util.concurrent.LinkedBlockingDeque.))
(defn- process-queue-item
[item]
(println ">> " item) ; this would be replaced by downloading `item`
(Thread/sleep queue-delay))
如果我在代码中包含(future (process-queue-item (take! my-queue)))
,那么在REPL我可以(offer! my-queue "something")
,我会立即打印“>>”。到现在为止还挺好!但是我需要队列在我的程序活动的整个时间内持续。我刚刚提到的(future ...)
调用可以将一个项目从队列中拉出来,一旦它可用,但是我想要一些能够持续观察队列的东西,并在有空的时候调用process-queue-item
。
另外,与通常的Clojure对并发的热爱相反,我想确保一次只发出一个请求,并且我的程序等待60秒才能发出每个后续请求。
我认为this Stack Overflow question是相关的,但我不确定如何调整它来做我想要的事情。如何连续轮询我的队列并确保一次只运行一个请求?
答案 0 :(得分:3)
以下是a project I did for fun的代码段。这不是完美的,但可以让你知道我是如何绕过“等待55秒的第一项”问题。它基本上通过承诺循环,使用期货立即处理事物或直到承诺“变得”可用。
(defn ^:private process
[queues]
(loop [[q & qs :as q+qs] queues p (atom true)]
(when-not (Thread/interrupted)
(if (or
(< (count (:promises @work-manager)) (:max-workers @work-manager))
@p) ; blocks until a worker is available
(if-let [job (dequeue q)]
(let [f (future-call #(process-job job))]
(recur queues (request-promise-from-work-manager)))
(do
(Thread/sleep 5000)
(recur (if (nil? qs) queues qs) p)))
(recur q+qs (request-promise-from-work-manager))))))
也许你可以做类似的事情?代码不是很好,可能需要重新编写才能使用lazy-seq
,但这只是我还没有完成的练习!
答案 1 :(得分:1)
这很可能是疯了但你总是可以使用这样的函数来创建一个减慢的懒惰序列:
(defn slow-seq [delay-ms coll]
"Creates a lazy sequence with delays between each element"
(lazy-seq
(if-let [s (seq coll)]
(do
(Thread/sleep delay-ms)
(cons (first s)
(slow-seq delay-ms (rest s)))))))
这基本上可以确保每个函数调用之间的延迟。
您可以使用以下内容,以毫秒为单位提供延迟:
(doseq [i (slow-seq 500 (range 10))]
(println (rand-int 10))
或者您可以将函数调用放在序列中,例如:
(take 10 (slow-seq 500 (repeatedly #(rand-int 10))))
显然,在上述两种情况下,您可以使用用于执行/触发下载的任何代码替换(rand-int 10)
。
答案 2 :(得分:1)
我最终滚动了我自己的小型图书馆,我称之为simple-queue。您可以阅读有关GitHub的完整文档,但这里是完整的源代码。 我不打算更新此答案,因此如果您想使用此库,请从GitHub获取源代码。
(ns com.github.bdesham.simple-queue)
(defn new-queue
"Creates a new queue. Each trigger from the timer will cause the function f
to be invoked with the next item from the queue. The queue begins processing
immediately, which in practice means that the first item to be added to the
queue is processed immediately."
[f & opts]
(let [options (into {:delaytime 1}
(select-keys (apply hash-map opts) [:delaytime])),
delaytime (:delaytime options),
queue {:queue (java.util.concurrent.LinkedBlockingDeque.)},
task (proxy [java.util.TimerTask] []
(run []
(let [item (.takeFirst (:queue queue)),
value (:value item),
prom (:promise item)]
(if prom
(deliver prom (f value))
(f value))))),
timer (java.util.Timer.)]
(.schedule timer task 0 (int (* 1000 delaytime)))
(assoc queue :timer timer)))
(defn cancel
"Permanently stops execution of the queue. If a task is already executing
then it proceeds unharmed."
[queue]
(.cancel (:timer queue)))
(defn process
"Adds an item to the queue, blocking until it has been processed. Returns
(f item)."
[queue item]
(let [prom (promise)]
(.offerLast (:queue queue)
{:value item,
:promise prom})
@prom))
(defn add
"Adds an item to the queue and returns immediately. The value of (f item) is
discarded, so presumably f has side effects if you're using this."
[queue item]
(.offerLast (:queue queue)
{:value item,
:promise nil}))
使用此队列返回值的示例:
(def url-queue (q/new-queue slurp :delaytime 30))
(def github (q/process url-queue "https://github.com"))
(def google (q/process url-queue "http://www.google.com"))
对q/process
的调用将会阻止,以便在两个def
语句之间有30秒的延迟。
纯粹用于副作用的使用此队列的示例:
(defn cache-url
[{url :url, filename :filename}]
(spit (java.io.File. filename)
(slurp url)))
(def url-queue (q/new-queue cache-url :delaytime 30))
(q/add url-queue {:url "https://github.com",
:filename "github.html"}) ; returns immediately
(q/add url-queue {:url "https://google.com",
:filename "google.html"}) ; returns immediately
现在,q/add
的呼叫立即返回。