def open(self): print("WebSocket opened") # Subject одновременно и observable, и observer self.subject = Subject() def send_response(x): self.write_message(json.dumps(x)) def on_error(ex): print(ex) user_input = self.subject.throttle_last( 1000 # На заданном временном промежутке получать последнее значение ).start_with( '' # Сразу же после подписки отправляет значение по умолчанию ).filter( lambda text: not text or len(text) > 2 ) interval_obs = Observable.interval( 60000 # Отдает значение раз в 60с (для периодического обновления) ).start_with(0) # combine_latest собирает 2 потока из запросов пользователя и временных # интервалов, срабатывает на любое сообщение из каждого потока self.combine_latest_sbs = user_input.combine_latest( interval_obs, lambda input_val, i: input_val ).do_action( # Срабатывает на каждый выпущенный элемент # Отправляет сообщение для очистки списка на фронтэнд lambda x: send_response('clear') ).flat_map( # В цепочку встраивается observable для получения списка self.get_data ).subscribe(send_response, on_error)
def open(self): print("WebSocket opened") self.write_message("connection opened") def send_response(x): self.write_message(json.dumps(x)) def on_error(ex): print(ex) self.subject = Subject() user_input = self.subject.throttle_last( 1000 # Given the last value in a given time interval ).start_with( '' # Immediately after the subscription sends the default value ).filter( lambda text: not text or len(text) > 2 ) interval_obs = Observable.interval( 60000 #refresh the value every 60 Seconds for periodic updates ).start_with(0) self.combine_latest_sbs = user_input.combine_latest( interval_obs, lambda input_val, i: input_val ).do_action( lambda x: send_response('clear') ).flat_map( self.get_data ).subscribe(send_response, on_error)
def test_interval_timespan_observer_throws(self): scheduler = TestScheduler() xs = Observable.interval(1, scheduler=scheduler) xs.subscribe(lambda x: _raise("ex")) with self.assertRaises(RxException): scheduler.start()
def test_interval_timespan_observer_throws(self): scheduler = TestScheduler() xs = Observable.interval(1, scheduler=scheduler) xs.subscribe(lambda x: _raise("ex")) try: return scheduler.start() except RxException: pass
def from_iterable_with_interval(cls, period, iterable, scheduler=None): """ Creates an Observable that polls iterator made from iterable every period millis. :param period: polling interval :param iterable: data source provider. :return: Observable instance """ iterator = iter(iterable) return Observable.interval(period, scheduler=scheduler).map(lambda v: next(iterator))
def main(): # start bluetooth scanning in the background new_bluetooth_thread = threading.Thread(target=scanner.watch) new_bluetooth_thread.daemon = True # stop if the program exits new_bluetooth_thread.start() # start listening door tag new_doortag_thread = threading.Thread(target=door_listener.listen) new_doortag_thread.daemon = True # stop if the program exits new_doortag_thread.start() # light on if pir detection or door opened door_listener.openDoorStream.merge(pirs.pirStream)\ .subscribe(LightOnObserver()) # light of after x second with no event door_listener.openDoorStream.merge(pirs.pirStream).debounce(parameters.lightup_duration*1000)\ .subscribe(LightOffObserver()) # send sms when unknown presence is detected pirs.pirStream.pausable(scanner.blueStream)\ .throttle_first(parameters.sms_min_interval_between_sms*1000).subscribe(SmsObserver('presence detected')) # send sms on open door door_listener.openDoorStream.pausable(scanner.blueStream)\ .throttle_first(parameters.sms_min_interval_between_sms*1000).subscribe(SmsObserver('door open')) # send sms on door vibe door_listener.vibeDoorStream.pausable(scanner.blueStream)\ .throttle_first(parameters.sms_min_interval_between_sms*1000).subscribe(SmsObserver('door vibe')) # turn siren when unknown presence is detected with both open door and pir # TODO: put timer duration in a variable door_listener.openDoorStream\ .join(pirs.pirStream, lambda x: Observable.timer(15*1000), lambda x: Observable.timer(15*1000), lambda x, y: True)\ .pausable(scanner.blueStream).subscribe(SirenClientObserver()) # take photo if pir detection or door opened or door vibe # TODO: more test on this # TODO: put camera interval in a variable door_listener.openDoorStream.merge(door_listener.vibeDoorStream).merge(pirs.pirStream) \ .select_many(Observable.interval(1000).take(20)).throttle_first(1000) \ .pausable(scanner.blueStream) \ .subscribe(CameraObserver()) # if the subscriber doesn't get the first blueStream event they will be paused by default # TODO: use a replay here instead of the following workaround scanner.scan() while True: # TODO: use a infinit loop instead # TODO: manage interruption time.sleep(1)
def create_new_html_on_md_file_changes_observable(file_name, scheduler): return Observable.interval(100, scheduler=scheduler).scan( check_changes, seed={'previous_modify_date': -1, 'changed': False, 'filename': file_name}).filter( lambda val: val['changed']).scan( create_new_html, seed={'file_name': file_name, 'html': '', 'changed_slide': (0,0)}).filter( lambda val: val['changed_slide'] != None)
if value % 2 == 0: timer_border = '+' # todo: change the on_next method to alternate the timer border between any two characters (hint: use value in some way) else: timer_border = 'x' def on_completed(self): print("minute stream completed!") def on_error(self, error): print("minute stream errored: " + "'" + str(error) + "'") # todo: fill in the following lines second_stream = Observable.interval( 1000 ) # todo: add a stream here that the observer will subscribe to that will print out the timer every second second_observer = secondObserver( ) # todo: add an observer here that will subscribe to this stream second_stream.subscribe( second_observer) # todo: subscribe the second observer to the stream minute_stream = Observable.interval( 60000 ) # todo: add a stream here that the observer will subscribe to that will change the timer border every minute (global timer_border) minute_observer = minuteObserver( ) # todo: add an observer here that will subscribe to this stream
from rx import Observable o1 = Observable.interval(1000) o2 = Observable.from_(["A", "B", "C"]) Observable.zip(o1,o2,lambda x,y:(x,y)) \ .subscribe(lambda x: print(x))
from rx import Observable from rx.testing import TestScheduler if __name__ == '__main__': test_scheduler = TestScheduler() Observable.interval(10, test_scheduler).take_until( Observable.timer(30)).subscribe(lambda s: print(s)) test_scheduler.start()
def resolve_count_seconds(root, info, up_to=5): return Observable.interval(1000)\ .map(lambda i: "{0}".format(i))\ .take_while(lambda i: int(i) <= up_to)
def schedule_say_hello(conn): Observable.interval(choice([300, 500, 1000, 2000, 3000]), scheduler=scheduler).subscribe(lambda value: say_hello())
''' Infinite loop _before_ the subscribe operation is blocking and killing it kills the entire program. Infinite loop _after_ the subscribe operation is blocking too, but killing doesn't kill the entire program, because the subscribe loop still runs. ''' import time from rx import Observable from rx.subjects import Subject subject = Subject() source = Observable.interval(1000) sub_subject1 = subject.subscribe( lambda v : print("Value published to observer 1: {0}".format(v)), lambda e : print("Error! {0}".format(e)), lambda : print("Completed!") ) sub_subject2 = subject.subscribe( lambda v : print("Value published to observer 2: {0}".format(v)), lambda e : print("Error! {0}".format(e)), lambda : print("Completed!") ) # This works as expected # while True:
from rx import Observable source1 = Observable.interval(1000).map(lambda i: "Source 1: {0}".format(i)) source2 = Observable.interval(500).map(lambda i: "Source 2: {0}".format(i)) source3 = Observable.interval(300).map(lambda i: "Source 3: {0}".format(i)) Observable.from_([source1,source2,source3]) \ .merge_all() \ .subscribe(lambda s: print(s)) # keep application alive until user presses a key input("Press any key to quit\n")
def _setup_window(self): self.resize(640, 320) self.move(350, 200) self.setWindowTitle("hello world") def random_stock(x): symbol_names = [['ABC', 'Abc Manufacturing'], ['DEF', 'Desert Inc'], ['GHI', 'Ghi Ghi Inc'], ['A', 'A Plus Consulting'], ['GS', 'Great Security Inc'], ['GO', 'Go Go Consulting']] stock = random.choice(symbol_names) return [ stock[0], stock[1], round(random.uniform(21, 22), 2), round(random.uniform(20, 21), 2) ] REFRESH_STOCK_INTERVAL = 100 if __name__ == '__main__': app = QApplication(sys.argv) scheduler = QtScheduler(QtCore) stock_prices = Observable.interval(REFRESH_STOCK_INTERVAL, scheduler).map(random_stock).publish() hello_world = HelloWorld(stock_prices_stream=stock_prices) hello_world.show() stock_prices.connect() sys.exit(app.exec_())
return Observable.from_(file) \ .flat_map(lambda s: Observable.from_(s.split())) \ .map(lambda w: re.sub(r'[^\w\s]', '', w)) \ .filter(lambda w: w != "") \ .map(lambda w: w.lower()) \ def word_counter(file_name): # count words using `group_by()` # tuple the word with the count return words_from_file(file_name) \ .group_by(lambda word: word) \ .flat_map(lambda grp: grp.count().map(lambda ct: (grp.key, ct))) # composes the above word_counter() into a dict def word_counter_as_dict(file_name): return word_counter(file_name).to_dict(lambda t: t[0], lambda t: t[1]) article_file = "blog.txt" # create a dict every three seconds, but only push if it changed Observable.interval(3000) \ .flat_map(lambda i: word_counter_as_dict(article_file)) \ .distinct_until_changed() \ .subscribe(lambda word_ct_dict: print(word_ct_dict)) # Keep alive until user presses any key input("Starting, press any key to quit\n")
def resolve_hello(root, info): return Observable.interval(3000).map(lambda i: "hello world!")
frames[0], frames[1], lambda x, y: "{} {}".format(x, y)) for f in frames[2:]: result = Observable.combine_latest( result, f, lambda x, y: "{} {}".format(x, y)) return result disk_icon = "^fn(FontAwesome)^fn() " cpu_icon = "^fn(material icons)^fn() " mem_icon = "^fn(material icons)^fn() " mail_icon = "^fn(material icons)^fn() " cal_icon = "^fn(material icons)^fn() " time = Observable.interval(6000) \ .map(lambda _: cal_icon+"{dt:%a} {dt:%b} {dt.day} {dt:%H}:{dt:%M}" .format(dt=datetime.now())) \ .distinct_until_changed() ys = Observable.interval(6000) \ .map(get_disks) \ documents = ys.map(lambda x: x[b"Documents"]).map(make_bar) sda2 = ys.map(lambda x: x[b"/dev/sda2"]).map(make_bar) disks = Observable.combine_latest(sda2, documents, lambda x, y: disk_icon + x + " " + y) \ .distinct_until_changed() cpu = Observable.interval(6000) \ .map(get_cpu) \ .map(lambda x: cpu_icon+make_bar(x))
from rx import Observable, Observer Observable.interval(55) \ .filter(lambda i : i%2 == 0) \ .subscribe(lambda i : print(i)) input("Enter para terminar\n")
def refresh_capabilities_in_background(self, period=5): Observable.interval(period * 1000, scheduler=_scheduler) \ .subscribe_on(_scheduler) \ .subscribe(lambda _: self.refresh_capabilities_from_remote(period))
from rx import Observable import time disposable = Observable.interval(1000) \ .map(lambda i: "{0} Mississippi".format(i)) \ .subscribe(lambda s: print(s)) # sleep 5 seconds so Observable can fire time.sleep(5) # disconnect the Subscriber print("Unsubscribing!") disposable.dispose() # sleep a bit longer to prove no more emissions are coming time.sleep(5)
from __future__ import print_function from rx import Observable foo = Observable.interval(100).take(5) foo.debounce(1000).subscribe(lambda s: print(s), on_completed=lambda : print("DONE")) input("ok\n")
def create(): return Observable.interval(1000, scheduler=scheduler)
from rx import Observable source1 = Observable.interval(1000).map(lambda s: "Source 1: {0}".format(s)) source2 = Observable.interval(500).map(lambda s: "Source 2: {0}".format(s)) source3 = Observable.interval(2000).map(lambda s: "Source 3: {0}".format(s)) Observable.merge(source1, source2, source3) \ .subscribe(lambda s: print(s)) input("Press key to quit\nq")
def schedule_say_hello(conn): sleep = choice([500, 600, 700]) Observable.interval( sleep, scheduler=scheduler).subscribe(lambda value: say_hello())
from rx import Observable if __name__ == '__main__': Observable.from_(range(2000)).buffer(Observable.interval(0.1)).subscribe( lambda buffer: print('#', len(buffer)))
def resolve_random_int(root, info): return Observable.interval(1000).map( lambda i: RandomType(seconds=i, random_int=random.randint(0, 500)))
def word_counter(file_name): # count words using `group_by()` # tuple the word with the count return words_from_file(file_name) \ .group_by(lambda word: word) \ .flat_map(lambda grp: grp.count().map(lambda ct: (grp.key, ct))) # composes the above word_counter() into a dict def word_counter_as_dict(file_name): return word_counter(file_name).to_dict(lambda t: t[0], lambda t: t[1]) # Schedule to create a word count dict every three seconds an article # But only re-print if text is edited and word counts change article_file = "bbc_news_article.txt" # create a dict every three seconds, but only push if it changed Observable.interval(3000) \ .flat_map(lambda i: word_counter_as_dict(article_file)) .distinct_until_changed() \ .subscribe(lambda word_ct_dict: print(word_ct_dict)) # Keep alive until user presses any key input("Starting, press any key to quit\n")
from rx import Observable from rx.subjects import Subject w_count = 0 def on_window(w): global w_count i = w_count w_count += 1 w_name = 'w%d' % i w.subscribe(lambda v: print(w_name, v), lambda ex: print(w_name, ex), lambda: print(w_name, 'completed')) source = Observable.interval(1000) closer = Subject() s = source.window(lambda: closer) s.subscribe(on_window, lambda ex: print('source', ex), lambda: print('source completed')) in_ = input('') while in_ != 'exit': closer.on_next(0) in_ = input('')
from rx import Observable import time source = Observable.interval(1000).publish() source.subscribe(lambda s: print("Subscriber 1: {0}".format(s))) source.connect() # sleep 5 seconds, then add another subscriber time.sleep(5) source.subscribe(lambda s: print("Subscriber 2: {0}".format(s))) input("Press any key to exit\n")
read_lat_line_from_file('test.csv').flat_map( lambda line: read_lat_line_from_file('test2.csv')).subscribe(print_value) print('window') # window is like a buffer print('window with count') Observable.from_( range(3)).window_with_count(2).flat_map(lambda x: x).subscribe(print_value) print('window with time') test_scheduler = TestScheduler() Observable.interval(50, test_scheduler).take_until( Observable.timer(100)).window_with_time(10).subscribe( lambda observable: observable.count().subscribe(print_value)) test_scheduler.start() print('combine latest') test_scheduler = TestScheduler() Observable.combine_latest( Observable.interval(1, test_scheduler).map(lambda x: 'a {}'.format(x)), Observable.interval(2, test_scheduler).map(lambda x: 'b {}'.format(x)), lambda a, b, : '{} {}'.format(a, b)).take_until( Observable.timer(5)).subscribe(print) # test_scheduler.start() print('-- zip') # test_scheduler = TestScheduler()
def create(): return Observable.interval(interval_time, scheduler)
# Import key libraries, classes and functions from rx import Observable from rx.testing import marbles, TestScheduler # Create a test scheduler test_scheduler = TestScheduler() # Function - print the observed values | To be subscribed to def print_value(value): print('{} is the value'.format(value)) # Observable - interval Observable.interval(10, test_scheduler).take_until( Observable.timer(30)).subscribe(print_value) # Start the test scheduler test_scheduler.start()
def inner(s): inds = Observable.interval(0) return s.zip(inds, lambda x, ind: ind) \ .filter(lambda ind: ind in beep_set)
def update_capabilities_in_background(self, period=10): Observable.interval(period * 1000, scheduler=_scheduler) \ .subscribe_on(_scheduler) \ .subscribe(lambda _: self.update_capabilities_from_remote())
from __future__ import print_function from rx import Observable foo = Observable.interval(100).take(5) foo.delay_with_selector(lambda x: Observable.interval(x * x * 1000).take(1)) \ .subscribe(on_next = lambda x: print(x), on_completed=lambda: print("complete")) input("ok\n")
from rx import Observable letters = Observable.of("Alpha", "Beta", "Gamma", "Delta", "Epsilon") intervals = Observable.interval(1000) Observable.zip(letters, intervals, lambda s, i: (s, i)) \ .subscribe(lambda t: print(t)) input("Press any key to quit\n") ''' Press any key to quit ('Alpha', 0) ('Beta', 1) ('Gamma', 2) ('Delta', 3) ('Epsilon', 4) '''
from rx import Observable # https://www.safaribooksonline.com/videos/reactive-python-for/9781491979006/9781491979006-video294990 obs1 = Observable.from_([1, 2, 445, 46, 2, 23, 5]) obs2 = Observable.from_([2, 3, 88, 14, 7, 1, 41]) Observable.merge(obs1, obs2).subscribe(lambda x: print(x)) obs1 = Observable.interval(1000).map(lambda i: "Source 1: {0}".format(i)) obs2 = Observable.interval(500).map(lambda i: "Source 2: {0}".format(i)) obs3 = Observable.interval(300).map(lambda i: "Source 3: {0}".format(i)) Observable.merge(obs1, obs2, obs3) \ .subscribe(lambda x: print(x)) Observable.from_([obs1, obs2, obs3]) \ .merge_all() \ .subscribe(lambda x: print(x)) items = ['"12/123/345/123/3/6', "8/3/1/6/9/05/", "4/3/6/8/9/4/3/67"] Observable.from_(items) \ .map(lambda s: Observable.from_(s.split('/'))) \ .merge_all() \ .subscribe(lambda i: print(i)) Observable.from_(items) \ .flat_map(lambda s: Observable.from_(s.split('/'))) \ .subscribe(lambda i: print(i))
from rx import Observable def frequent_firstnames_from_db(file_name): file = open(file_name) # collect and push only the frequent firstnames return Observable.from_(file) \ .flat_map(lambda content: content.split(', ')) \ .filter(lambda name: name!='') \ .map(lambda name: name.split()[0]) \ .group_by(lambda firstname: firstname) \ .flat_map(lambda grp: grp.count().map(lambda ct: (grp.key, ct))) \ .filter(lambda name_and_ct: name_and_ct[1] > 3) db_file = "people.txt" # Emit data every 5 seconds Observable.interval(5000) \ .flat_map(lambda i: frequent_firstnames_from_db(db_file)) \ .subscribe(lambda value: print(str(value))) # Keep alive until user presses any key input("Starting... Press any key to quit\n")
def create_app(): def setup_logging(): handler = StreamHandler(stream=sys.stdout) handler.setLevel(config.log_level) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) flask_app.logger.addHandler(handler) flask_app.logger.setLevel(config.log_level) flask_app.logger.name = "docker_enforcer" flask_app = Flask(__name__) if not flask_app.debug: setup_logging() flask_app.logger.info( "Starting docker-enforcer v{0} with docker socket {1}".format( config.version, config.docker_socket)) task_scheduler = NewThreadScheduler() # task_scheduler = ThreadPoolScheduler(multiprocessing.cpu_count()) if config.run_start_events: events = Observable.from_iterable(docker_helper.get_events_observable()) \ .observe_on(scheduler=task_scheduler) \ .where(lambda e: is_configured_event(e)) \ .map(lambda e: e['id']) \ .map(lambda cid: docker_helper.check_container(cid, CheckSource.Event, remove_from_cache=True)) if config.run_periodic: periodic = Observable.interval(config.interval_sec * 1000) if config.immediate_periodical_start: flask_app.logger.debug("Run periodic immediately") periodic = periodic.start_with(-1) periodic = periodic.observe_on(scheduler=task_scheduler) \ .map(lambda _: docker_helper.check_containers(CheckSource.Periodic)) \ .flat_map(lambda c: c) detections = Observable.empty() if config.run_start_events: detections = detections.merge(events) if config.run_periodic: detections = detections.merge(periodic) verdicts = detections \ .map(lambda container: judge.should_be_killed(container)) \ .where(lambda v: v.verdict) threaded_verdicts = verdicts \ .retry() \ .subscribe_on(task_scheduler) \ .publish() \ .auto_connect(2) if not config.run_start_events and not config.run_periodic: flask_app.logger.info( "Neither start events or periodic checks are enabled. Docker Enforcer will be working in " "authz plugin mode only.") else: killer_subs = threaded_verdicts.subscribe(jurek) trigger_subs = threaded_verdicts.subscribe(trigger_handler) def on_exit(sig, frame): flask_app.logger.info("Stopping docker monitoring") if config.run_start_events or config.run_periodic: killer_subs.dispose() trigger_subs.dispose() flask_app.logger.debug("Complete, ready to finish") quit() signal.signal(signal.SIGINT, on_exit) signal.signal(signal.SIGTERM, on_exit) return flask_app
# calculate number of CPU's, then create a ThreadPoolScheduler with that number of threads optimal_thread_count = multiprocessing.cpu_count() pool_scheduler = ThreadPoolScheduler(optimal_thread_count) # Create Process 1 Observable.of("Alpha", "Beta", "Gamma", "Delta", "Epsilon") \ .map(lambda s: intense_calculation(s)) \ .subscribe_on(pool_scheduler) \ .subscribe(on_next=lambda s: print("PROCESS 1: {0} {1}".format(current_thread().name, s)), on_error=lambda e: print(e), on_completed=lambda: print("PROCESS 1 done!")) # Create Process 2 Observable.range(1, 10) \ .map(lambda s: intense_calculation(s)) \ .subscribe_on(pool_scheduler) \ .subscribe(on_next=lambda i: print("PROCESS 2: {0} {1}".format(current_thread().name, i)), on_error=lambda e: print(e), on_completed=lambda: print("PROCESS 2 done!")) # Create Process 3, which is infinite Observable.interval(1000) \ .map(lambda i: i * 100) \ .observe_on(pool_scheduler) \ .map(lambda s: intense_calculation(s)) \ .subscribe(on_next=lambda i: print("PROCESS 3: {0} {1}".format(current_thread().name, i)), on_error=lambda e: print(e)) input("Press any key to exit\n")
def schedule_say_hello(conn): Observable.interval(choice(300, 500, 1000, 2000, 3000), scheduler=scheduler) \ .subscribe(lambda s: say_hello())
from rx import Observable Observable.interval(1000) \ .map(lambda i: "{0} Mississippi".format(i)) \ .subscribe(lambda s: print(s)) input("Press any key to quit\n")
See rx-stream-pacing Two ^C become necessary to stop this script - why? ''' from rx import Observable import APIReaderTwitter as Twitter try: import json except ImportError: import simplejson as json def pretty_print(element): print(json.dumps(element, indent=4)) def is_delete(element): return not "delete" in element # Generate an interval sequece, firing once each second tick = Observable.interval(1000) # Publish an event from Twitter each tick as a minimum # If the twitter stream is empty it will just wait for an event to come source = Observable.from_(Twitter.get_iterable()).zip( tick, lambda t, _: t ) # Print each element in intervals, waits a minimum of 1s between events source.filter(is_delete).subscribe(pretty_print)