Make the Subject run operators synchronously

Viewed 16

If it is possible to make the first one has the same result as the second?

from reactivex import operators as op
import reactivex as rx

first = rx.Subject()
res = []
s1 = first.pipe(op.group_by(lambda i: len(i)))
s2 = s1.pipe(op.flat_map(lambda grp: grp.pipe(op.to_list())))
s2.subscribe(lambda i: res.append(i))

first.on_next('app')
sleep(1)
first.on_next('ban')
sleep(1)
first.on_next('cpop')
print(res) #[]
res = []
rx.from_(['app', 'ban', 'cpop']).pipe(op.group_by(lambda i: len(i))).pipe(op.flat_map(lambda grp: grp.pipe(op.to_list()))).subscribe(lambda i: res.append(i))
print(res) #[['app', 'ban'], ['cpop']]

I suspect that the first one runs asynchronously, it runs the last step while first hasn't started the sending process.

0 Answers
Related