每当任何输入打勾时,我都会使用Reactive Extensions来combine_latest
执行操作。问题是,如果多个输入同时滴答,然后combine_latest
在每个单独的输入滴答后触发多次。这会让人头疼,因为combine_latest
它实际上是用过时的值虚假地滴答作响。
最小工作示例,其中fast
每 10 毫秒一个slow
可观察的滴答声,每 30 毫秒一个可观察的滴答声:
from rx.concurrency import HistoricalScheduler
from rx import Observable
from __future__ import print_function
scheduler = HistoricalScheduler(initial_clock=1000)
fast = Observable.generate_with_relative_time(1, lambda x: True, lambda x: x + 1, lambda x: x, lambda x: 10, scheduler=scheduler)
slow = Observable.generate_with_relative_time(3, lambda x: True, lambda x: x + 3, lambda x: x, lambda x: 30, scheduler=scheduler)
Observable.combine_latest(fast, slow, lambda x, y: dict(time=scheduler.now(), fast=x, slow=y)).subscribe(print)
scheduler.advance_to(1100)
每 30 毫秒,当fast
和slow
同时滴答时,combine_latest
不希望地触发两次——输出如下:
{'slow': 3, 'fast': 2, 'time': 1030} # <- This shouldn't be here
{'slow': 3, 'fast': 3, 'time': 1030}
{'slow': 3, 'fast': 4, 'time': 1040}
{'slow': 3, 'fast': 5, 'time': 1050}
{'slow': 6, 'fast': 5, 'time': 1060} # <- This shouldn't be here
{'slow': 6, 'fast': 6, 'time': 1060}
{'slow': 6, 'fast': 7, 'time': 1070}
{'slow': 6, 'fast': 8, 'time': 1080}
{'slow': 9, 'fast': 8, 'time': 1090} # <- This shouldn't be here
{'slow': 9, 'fast': 9, 'time': 1090}
{'slow': 9, 'fast': 10, 'time': 1100}
如何防止combine_latest
虚假滴答作响?