1

如果我有这样的事情:

scheduler = EventLoopScheduler()

obs1 = rx.range(0, 10)
obs2 = rx.range(20, 30).pipe(ops.combine_latest(obs1))

obs2.subscribe(lambda it: print(it), scheduler=scheduler)

time.sleep(5)

它给了我这样的输出:

(21, 0)
(21, 1)
(22, 1)
(22, 2)

不过,如何将其扩展到 2 个以上的可观察对象?例如,如果我这样做:

scheduler = EventLoopScheduler()

obs1 = rx.range(0, 10)
obs2 = rx.range(20, 30).pipe(ops.combine_latest(obs1))
obs3 = rx.range(40, 50).pipe(ops.combine_latest(obs2))

obs3.subscribe(lambda it: print(it), scheduler=scheduler)

time.sleep(5)

我得到:

(41, (20, 0))
(41, (21, 0))
(42, (21, 0))
(42, (21, 1))
(42, (22, 1))
(43, (22, 1))
(43, (22, 2))
(43, (23, 2))

但是,我真正想要的是平面列表中的最新 3 个,例如:

(41, 20, 0)
(41, 21, 0)
etc

我怎样才能做到这一点?

4

1 回答 1

1

弄清楚了:

scheduler = EventLoopScheduler()

obs1 = rx.range(0, 10)
obs2 = rx.range(20, 30)
obs3 = rx.range(30, 40)

rx.combine_latest(obs1, obs2, obs3).subscribe(lambda it: print(it), scheduler=scheduler)

time.sleep(5)
于 2020-05-10T15:17:32.747 回答