As an example for a class, I'm trying to figure out the most robust way to create a hot Observable
that emits current prices for a given stock ticker, but only have it emit a price when it changes. The best I could come up with on my own is create an interval source Observable
that will query Google Finance every 3 seconds, parse out the price, and only push it forward if the value has changed via distinct_until_changed()
.
from threading import Semaphore
from rx import Observable
from googlefinance import getQuotes
import json
Observable.interval(3000) \
.map(lambda i: json.dumps(getQuotes('AAPL'))) \
.map(lambda s: json.loads(s)[0]) \
.map(lambda dict: float(dict["LastTradePrice"])) \
.distinct_until_changed() \
.subscribe(print)
sema = Semaphore(0)
sema.acquire()
Does anyone know of a more efficient way to do this, and not have to re-query at an interval but rather have the source be triggered when the price actually changes? If it's overly complicated and requires some specialized library, I'll stick with what I have. I just feel like I am missing a much better way to do this...