0

我有一个sourceobservable,我订阅了一个logger观察者来进行日志记录。

我也订阅了,source所以我可以执行计算。当我的计算完成后,我已经完成source并且我想处理logger

             +-------------------+
             |                   |
   +---------+ source observable +--------+
   |         |                   |        |
   |         +-------------------+        |
   |                                      |
   |                                      |
+--v---------------+         +------------v--------+
|                  |         |                     |
|     logger       |         |    computations     |
|    (observer)    |         |    (observable)     |
+-------^----------+         +-----------+---------+
        |                                |
        |                                |
        |        dispose logger          |
        +--------------------------------+
            when computations completed

但是,logger并没有在正确的时间完全处理——通常会发生一两个额外的滴答声:

MWE

from rx import Observable

# Some source
source = Observable.interval(1)

# Create logger for source
logged = []
logger = source.subscribe(logged.append)

# Now do stuff/computations with source
calculated = source.map(lambda x: x**2).take_while(lambda x: x < 20)

# Output computed values and stop logging when we're done with our computation
calculated.subscribe(print, print, logger.dispose)

# I expect only values that passed through our computation to have been logged
# The last value should be 5 because 5**2 = 25 which is larger than 20
# which in turn causes our computation to terminate
assert logged == [0, 1, 2, 3, 4, 5], logged

但我得到:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 3035, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-54-e8cb1fb583bf>", line 1, in <module>
    assert logged == [0, 1, 2, 3, 4, 5], logged
AssertionError: [0, 1, 2, 3, 4, 5, 6, 7]

7是如何被记录的?我们的计算应该在发出 5 次后终止source,此时logger会处理掉。

我究竟做错了什么?

4

1 回答 1

0

这是线程同步问题。interval()运算符启动新线程以on_next()在指定的时间间隔内调用。处理订阅后,其他线程检测到此信号并停止工作需要时间。一毫秒接近它所花费的时间。

为了记录通过反应链传递的消息,将记录功能直接插入该链更可靠:

logged = []
def logger(x):
    logged.append(x)
    return x

calculated = source \
    .map(logger) \
    .map(lambda x: x**2) \
    .take_while(lambda x: x < 20) \
    .subscribe(print, print)
于 2017-01-13T09:37:32.633 回答