rxjava和clojure异步之谜:未来承诺和代理,天哪

rxjava和clojure异步之谜:未来承诺和代理,天哪,clojure,reactive-programming,netflix,rx-java,Clojure,Reactive Programming,Netflix,Rx Java,我为这封信的长度提前道歉。我花了相当长的时间把它缩短,这是我能做到的最小的 我有一个秘密,非常感谢你的帮助。这个谜团来自于我在Clojure中写的一个rxjavaobservative观测者的行为,这个观测者是从在线样本中抄袭出来的几个简单的observatables 一个可观察对象同步地向其观察者的onNext处理程序发送消息,而我假定的有原则的观察者的行为与预期的一样 另一个可观察对象通过Clojurefuture在另一个线程上异步执行相同的操作。完全相同的观察者不会捕获发布到其onNext

我为这封信的长度提前道歉。我花了相当长的时间把它缩短,这是我能做到的最小的

我有一个秘密,非常感谢你的帮助。这个谜团来自于我在Clojure中写的一个rxjava
observative
观测者的行为,这个观测者是从在线样本中抄袭出来的几个简单的
observatable
s

一个可观察对象同步地向其观察者的
onNext
处理程序发送消息,而我假定的有原则的观察者的行为与预期的一样

另一个可观察对象通过Clojure
future
在另一个线程上异步执行相同的操作。完全相同的观察者不会捕获发布到其
onNext
的所有事件;它似乎只是在尾部丢失了随机数目的消息

在等待
promise
d
onCompleted
的到期与等待发送到
代理
收集器的所有事件的到期之间,存在一种有意的竞争。如果
promise
获胜,我希望看到
onCompleted
false
代理中可能出现的短队列。如果
代理
获胜,我希望看到
onCompleted
true
以及
代理
队列中的所有消息。我不期望的一个结果是
true
对于
onCompleted
和来自
代理的短队列。但是,墨菲不睡觉,这正是我所看到的。我不知道是垃圾收集出了问题,还是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#)))
最后,对我的观察者说。我使用
代理
来收集任何可观察的
onNext
发送的消息。我使用一个
atom
来收集一个潜在的
onError
。我对
未完成的
使用
承诺
,以便观察者外部的消费者可以等待它

(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))))
现在,这是一个同步观测。它将25条消息从观察者的
onNext
喉头向下传送,然后调用他们的
onCompleted
s

(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}
----------------
下面是一个异步可观察对象,它只在
未来的
线程上执行完全相同的操作:

(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
,这意味着
承诺没有超时;但只有一些异步消息。我们看到的消息的实际数量因运行而异,这意味着存在一些并发现象。谢谢

----------------
{: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}
----------------

代理上的
等待
意味着将阻止当前线程,直到所有操作都被调度 远离(此线程或代理)的代理已发生,这意味着在等待结束后,可能还有其他线程可以向代理发送消息,这就是您的情况。等待代理结束后,在映射中的
:onNext
键中取消其值,然后等待完成的承诺,该承诺在等待后变为真,但与此同时,一些其他消息被发送到代理,以收集到向量中

您可以通过将
:onCompleted
键作为映射中的第一个键来解决此问题,这基本上意味着等待完成,然后等待代理,因为此时不再有对代理的
发送
调用发生在as已经接收到onCompleted之后

{:onCompleted (deref onCompletedCollector 1000 false)
 :onNext      (do (await-for 0 onNextCollector)
                                 @onNextCollector)
 :onError     @onErrorCollector
 }