对于本注释的篇幅,我提前致歉。我花了相当多的时间使它变得更短,而这却变得尽可能的小。
我有一个谜,感谢您的帮助。这个奥秘来自于我在Clojure中写的rxjava observer
的行为,该行为是根据在线示例中编写的几个简单的observable
编写的。
一个可观察到的对象将消息同步发送到其观察者的onNext
处理程序,而我假定的有原则的观察者的行为符合预期。
另一个可观察到的对象通过Clojure future
在另一个线程上异步执行相同的操作。完全相同的观察者不会捕获发布到其onNext
的所有事件;它似乎只是在尾部丢失了随机数量的消息。
在等待promise
和onCompleted
的等待期与等待发送到agent
收集器的所有事件的期满之间存在以下故意竞争。如果promise
获胜,我希望看到false
的onCompleted
以及agent
中可能很短的队列。如果agent
获胜,我希望看到true
的onCompleted
以及agent
队列中的所有消息。我不希望看到的一个结果是true
的onCompleted
和agent
中的一小段队列。但是,墨菲不 sleep ,而这正是我所看到的。我不知道垃圾收集是否有问题,或者是否对Clojure的STM有一些内部排队,或者我的愚蠢,或者其他所有原因。
我在这里按自包含格式的顺序显示源,以便可以直接通过lein repl
运行它。有三种方法可以避免:首先,leiningen项目文件project.clj
,它声明对Netflix rxjava的0.9.0
版本的依赖:
(defproject expt2 "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]
[com.netflix.rxjava/rxjava-clojure "0.9.0"]]
:main expt2.core)
现在,命名空间和Clojure需求以及Java导入:
(ns expt2.core
(:require clojure.pprint)
(:refer-clojure :exclude [distinct])
(:import [rx Observable subscriptions.Subscriptions]))
最后,一个用于输出到控制台的宏:
(defmacro pdump [x]
`(let [x# ~x]
(do (println "----------------")
(clojure.pprint/pprint '~x)
(println "~~>")
(clojure.pprint/pprint x#)
(println "----------------")
x#)))
最后,给我的观察者。我使用
agent
收集任何可观察到的onNext
发送的消息。我使用atom
收集潜在的onError
。我将promise
用作onCompleted
,以便观察者外部的消费者可以等待它。(defn- subscribe-collectors [obl]
(let [;; Keep a sequence of all values sent:
onNextCollector (agent [])
;; Only need one value if the observable errors out:
onErrorCollector (atom nil)
;; Use a promise for 'completed' so we can wait for it on
;; another thread:
onCompletedCollector (promise)]
(letfn [;; When observable sends a value, relay it to our agent"
(collect-next [item] (send onNextCollector (fn [state] (conj state item))))
;; If observable errors out, just set our exception;
(collect-error [excp] (reset! onErrorCollector excp))
;; When observable completes, deliver on the promise:
(collect-completed [ ] (deliver onCompletedCollector true))
;; In all cases, report out the back end with this:
(report-collectors [ ]
(pdump
;; Wait for everything that has been sent to the agent
;; to drain (presumably internal message queues):
{:onNext (do (await-for 1000 onNextCollector)
;; Then produce the results:
@onNextCollector)
;; If we ever saw an error, here it is:
:onError @onErrorCollector
;; Wait at most 1 second for the promise to complete;
;; if it does not complete, then produce 'false'.
;; I expect if this times out before the agent
;; times out to see an 'onCompleted' of 'false'.
:onCompleted (deref onCompletedCollector 1000 false)
}))]
;; Recognize that the observable 'obl' may run on another thread:
(-> obl
(.subscribe collect-next collect-error collect-completed))
;; Therefore, produce results that wait, with timeouts, on both
;; the completion event and on the draining of the (presumed)
;; message queue to the agent.
(report-collectors))))
现在,这是一个同步可观察到的东西。它从观察者的
onNext
喉咙中抽取25条消息,然后将其称为onCompleted
。(defn- customObservableBlocking []
(Observable/create
(fn [observer] ; This is the 'subscribe' method.
;; Send 25 strings to the observer's onNext:
(doseq [x (range 25)]
(-> observer (.onNext (str "SynchedValue_" x))))
; After sending all values, complete the sequence:
(-> observer .onCompleted)
; return a NoOpSubsription since this blocks and thus
; can't be unsubscribed (disposed):
(Subscriptions/empty))))
我们让观察者订阅这个可观察到的信息:
;;; The value of the following is the list of all 25 events:
(-> (customObservableBlocking)
(subscribe-collectors))
它可以按预期工作,并且我们在控制台上看到以下结果
{:onNext (do (await-for 1000 onNextCollector) @onNextCollector),
:onError @onErrorCollector,
:onCompleted (deref onCompletedCollector 1000 false)}
~~>
{:onNext
["SynchedValue_0"
"SynchedValue_1"
"SynchedValue_2"
"SynchedValue_3"
"SynchedValue_4"
"SynchedValue_5"
"SynchedValue_6"
"SynchedValue_7"
"SynchedValue_8"
"SynchedValue_9"
"SynchedValue_10"
"SynchedValue_11"
"SynchedValue_12"
"SynchedValue_13"
"SynchedValue_14"
"SynchedValue_15"
"SynchedValue_16"
"SynchedValue_17"
"SynchedValue_18"
"SynchedValue_19"
"SynchedValue_20"
"SynchedValue_21"
"SynchedValue_22"
"SynchedValue_23"
"SynchedValue_24"],
:onError nil,
:onCompleted true}
----------------
这是一个异步的Observable,它仅在
future
的线程上执行完全相同的操作:(defn- customObservableNonBlocking []
(Observable/create
(fn [observer] ; This is the 'subscribe' method
(let [f (future
;; On another thread, send 25 strings:
(doseq [x (range 25)]
(-> observer (.onNext (str "AsynchValue_" x))))
; After sending all values, complete the sequence:
(-> observer .onCompleted))]
; Return a disposable (unsubscribe) that cancels the future:
(Subscriptions/create #(future-cancel f))))))
;;; For unknown reasons, the following does not produce all 25 events:
(-> (customObservableNonBlocking)
(subscribe-collectors))
但是,令人惊讶的是,这是我们在控制台上看到的:
true
表示onCompleted
,表示promise
DID NOT TIME-OUT;但只有一些异步消息。我们看到的实际消息数量因运行而异,这意味着在运行中存在一些并发现象。线索表示赞赏。----------------
{:onNext (do (await-for 1000 onNextCollector) @onNextCollector),
:onError @onErrorCollector,
:onCompleted (deref onCompletedCollector 1000 false)}
~~>
{:onNext
["AsynchValue_0"
"AsynchValue_1"
"AsynchValue_2"
"AsynchValue_3"
"AsynchValue_4"
"AsynchValue_5"
"AsynchValue_6"],
:onError nil,
:onCompleted true}
----------------
最佳答案
代理上的await-for
表示阻止当前线程,直到由此调度的所有操作为止
距离(从此线程或代理程序到代理程序)的距离很远(这意味着可能发生的情况是,在您的等待结束之后,还有其他线程可以向代理程序发送消息,而您的情况就是这样。在您对代理的等待结束并且您在 map 的:onNext
key 中取消引用了它的值之后,您等待等待完成的 promise 完成,但是在等待之后事实却是如此,但与此同时,其他一些消息也被分发给了要收集到媒介中的媒介。
您可以通过将:onCompleted
键作为映射中的第一个键来解决此问题,这基本上意味着等待完成,然后等待代理coz,到那时,在已经收到onCompleted之后,不再对代理进行send
调用。
{:onCompleted (deref onCompletedCollector 1000 false)
:onNext (do (await-for 0 onNextCollector)
@onNextCollector)
:onError @onErrorCollector
}