what's the elegant way to convert a rx.Observable object to a 'normal' object in a function?
e.g.:
def foo():
return rx.Observable.just('value').subscribe(<some magic here>)
>>> print(foo())
# expected:
# value
# however get:
# <rx.disposables.anonymousdisposable.AnonymousDisposable at SOME ADDRESS>
I understand that return of subscribe is a disposable object, and a 'ugly' way to implement this is:
class Foo:
def __init__(self):
self.buffer = None
def call_kernel(self):
rx.Observable.just('value').subscribe(lambda v: self.buffer = v)
def __call__(self):
self.call_kernel()
return self.buffer
>>> Foo()
# get:
# value
Is there any better way to do this?
Thanks.
Take a look at
Observable::to_blocking()
: it creates aBlockingObservable
which is coercible to a list containing all the emitted items. For your example:I'd also like to point out your second solution is dangerous, because there is no guarantee exactly when values will be emitted, and your
__call__
relies on'value'
to emit immediately.