def start(self): logging.debug("in MyObservable.start") rx.interval(1.0).pipe( ops.subscribe_on(self.scheduler), ops.flat_map(lambda i: self.fetch_subjects())).subscribe( on_next=lambda v: self.subject.on_next(v), on_error=lambda e: print_error("Error in interval loop", e))
def __init__(self, name, key=None, interval=120, scheduler=None, auto_timestamp=False): """ Automatic data store as pidle. The on_next message must be of pandas.Dataframe :param auto_timestamp: :param store_name: file name of the stored file. The target_dir option is respected in configs.ini :param interval: if None, store every time when new data come. Otherwise, only store after interval seconds. """ super().__init__(name, on_next=self.on_new_data) self.auto_timestamp = auto_timestamp self.key = key self.save_path = os.path.join(self.config["data"]["base"], name + ".pkl") self.interval = interval self.df = pd.DataFrame() # for timed data store self.new_data_flag = False if interval is not None: self.logger.info(f"storing data every {self.interval} seconds") rx.interval(interval, scheduler).pipe( operators.filter(lambda *args: self.new_data_flag)).subscribe( lambda *args: self.save_data())
def start(self): self.fps = self.config["fps"] rx.interval(1 / self.fps).pipe( operators.map(lambda x: self.generate_image()), operators.map( lambda arr: AcquiredImage(arr, datetime.now().timestamp())), operators.take_until(self._stop), ).subscribe(ErrorToConsoleObserver(self.next_image)) self.running = True super().start()
def configure_timed_read(self): interval = self.config.getfloat("fp50", "interval") if interval > 0: logger.info("Configuring timed read") # enabled rx.interval(interval, scheduler=NewThreadScheduler()).pipe( operators.flat_map(lambda x: self.control.get_power()), operators.map(lambda x: self.upload_power(x)), operators.delay(self.config.getfloat("fp50", "query_delay")), operators.flat_map( lambda x: self.control.get_internal_temperature()), operators.map(lambda x: self.upload_internal_temperature(x)), operators.catch(error_handler)).subscribe()
def test_interval_timespan_observer_throws(self): scheduler = TestScheduler() xs = rx.interval(1) xs.subscribe(lambda x: _raise("ex"), scheduler=scheduler) with self.assertRaises(RxException): scheduler.start()
async def demo(): one_second = timedelta(seconds=1) max_field_count = 1000 fail_every = 5 data_entry_service = InMemoryDataEntryService( field_count=max_field_count, create_interval=timedelta(milliseconds=1), fail_annotate_every=fail_every, ) annotation_service = InMemoryAnnotationService( publish_annotation=data_entry_service.annotate_field, republish_interval=one_second, fail_annotate_every=fail_every, fail_ack_every=fail_every, ) data_entry_service.start_publications( publish=annotation_service.annotate, acknowledge=annotation_service.acknowledge, interval=one_second, ) created_field_count = 0 acked_annotion_count = 0 unannotated_field_count = 0 unacked_annotion_count = 0 def consistent(_) -> bool: return (created_field_count == max_field_count and unannotated_field_count == 0 and unacked_annotion_count == 0) def inconsistent(_) -> bool: return not consistent(_) def eval_state(counter: int): nonlocal created_field_count nonlocal acked_annotion_count nonlocal unannotated_field_count nonlocal unacked_annotion_count created_field_count = data_entry_service.field_count acked_annotion_count = len(annotation_service.acknowledged_annotations) unacked_annotion_count = len( annotation_service.unacknowledged_annotations) unannotated_field_count = len(data_entry_service.unannotated_fields) print(f"Iteration: {counter + 1}") print(f"Field Count: {created_field_count}") print(f"Acked Annotation Count: {acked_annotion_count}") print(f"Unannotated Field Count: {unannotated_field_count}") print(f"Unacked Annotation Count: {unacked_annotion_count}") print("") if consistent(None): print("---------------") print("Eventually has arrived :)") print("---------------") sched = rx.interval(timedelta(seconds=1)) composed = sched.pipe(take_while(inconsistent)) composed.subscribe(eval_state)
def test_interval_timespan_observer_throws(self): scheduler = TestScheduler() xs = rx.interval(1) xs.subscribe(lambda x: _raise("ex"), scheduler=scheduler) with self.assertRaises(RxException): scheduler.start()
def main(): gme = yf.Ticker("GME") data = interval(5.0).pipe(switch_map(lambda i: get_yf_data(gme)), ) data.subscribe( on_next=next, on_error=err, on_completed=next, )
def on_start(self): self.sock = sock = socket.socket() try: sock.connect(('127.0.0.1', 60000)) except ConnectionRefusedError: print("The Oware server is not running") raise SystemExit sock.setblocking(False) sched = rx.concurrency.ThreadPoolScheduler() rx.interval(0.5, sched).pipe( ops.map(lambda _: try_recv(sock)), ops.map(ByteFIFO().add), ops.flat_map(self.decode) ).subscribe_(on_next = self.receive, on_error = print)
def download_file(f, dest): total_bytes = int(f['size']) def next_chunk(downloader): status, done = downloader.next_chunk() logging.debug('downloaded chunk from {} to {}: {}'.format( file, destination, status)) return 1.0 if done else status.progress() def download_from_drive(destination_file): def create_downloader(): request = get_service(credentials).files().get_media( fileId=f['id']) return MediaIoBaseDownload(fd=destination_file, request=request, chunksize=CHUNK_SIZE) downloader = create_downloader() return forever().pipe( map(lambda _: next_chunk(downloader)), take_while(lambda p: p < 1, inclusive=True), flat_map(lambda p: progress( default_message= 'Downloaded {downloaded_files} of {total_files} files ({downloaded} of {total})', message_key='tasks.drive.download_folder', downloaded_bytes=int(total_bytes * p), downloaded=format_bytes(int(total_bytes * p)), total_bytes=total_bytes, total=format_bytes(total_bytes), file=f))) def action(): return using_file(file=dest, mode='wb', to_observable=download_from_drive) os.makedirs(os.path.dirname(dest), exist_ok=True) initial_progress = progress( default_message= 'Downloaded {downloaded_files} of {total_files} files ({downloaded} of {total})', message_key='tasks.drive.download_folder', downloaded_bytes=0, downloaded='0 bytes', total_bytes=total_bytes, total=format_bytes(total_bytes), file=f) touch_stream = interval(TOUCH_PERIOD).pipe( flat_map(lambda _: touch(credentials, f))) download_stream = enqueue(credentials, queue=_drive_downloads, action=action, retries=retries, description='Download {} to {}'.format( f, dest)).pipe(aside(touch_stream)) return concat(initial_progress, download_stream, delete_downloaded(f))
def main(): dbus = DBus.new_tcp_connection('192.168.178.137') service_name = 'com.victronenergy.example' def always(_): return True dbus.publish_ve_property(service_name, '/ProductName', 'Example Product') dbus.publish_ve_property(service_name, '/YouCannotEditMe', 'try it') dbus.publish_ve_property(service_name, '/EnterNumberBetween0and10', 5, accept_change=lambda n: 0 <= float(n) <= 10) dbus.publish_ve_property(service_name, '/EnterSalutation', 'Hello', accept_change=always) dbus.publish_ve_property(service_name, '/EnterYourName', '', accept_change=always) dbus.publish_ve_property(service_name, '/Greetings', 'please enter a name and a salutation') def update_counter(i): dbus.publish_ve_property(service_name, '/Counter', i) rx.interval(timedelta(seconds=1)).subscribe(update_counter) def greeter(s_n: Tuple[VeProperty, VeProperty]): s, n = s_n salutation = s.text name = n.text greeting = f'{salutation} {name}' if salutation and name else 'please enter a name and a salutation' dbus.publish_ve_property(service_name, '/Greetings', greeting) salutation = dbus.observe_ve_property(service_name, '/EnterSalutation') name = dbus.observe_ve_property(service_name, '/EnterYourName') rx.combine_latest(salutation, name).subscribe(greeter) dbus.run_forever()
def __init__(self, client: MQTTClientWrapper, name="fp50", config=None): super().__init__(name, config=config) self.client = client self.topic_base = self.config["waterBath"][name]["topicBase"] self.topic = self.topic_base + "/setpoint" self.message_subject = Subject() self.client.subscribe(self.topic_base + "/crystallizer_temperature") self.client.subscribe(self.topic_base + "/setpoint") self.interval_scheduler = NewThreadScheduler() def update(x, scheduler=None): self.client.publish(self.topic_base + "/crystallizer_temperature", None) rx.interval(self.config["waterBath"][name]["interval"], self.interval_scheduler).subscribe(update) def convert(x): payloads = [xx[2].payload for xx in x] for p in payloads: if not p: # skipping conversion request return None return { # "power": float(payloads[0]), # "internal_temperature": float(payloads[1]), "crystallizer_temperature": float(payloads[0]), "setpoint": float(payloads[1]), } rx.combine_latest( from_callback( self.client.message_callback_add)(self.topic_base + "/crystallizer_temperature"), from_callback(self.client.message_callback_add)(self.topic_base + "/setpoint"), ).pipe(operators.map(convert), operators.filter(lambda x: x is not None), operators.debounce(0.6)).subscribe(self.message_subject)
def spot_instance_check_observable(interval_seconds: float = 1.0) -> rx.Observable: """ スポットインスタンスのステータスをinterval秒ごとにurlを叩いてチェックしに行くobservableを返す関数 :param interval_seconds: interval seconds """ return rx.interval(interval_seconds).pipe( ops.map(access_spot_instance_state), ops.filter(lambda resp: resp.status_code != 404), ops.map(spot_instance_state_response_to_checker_message), ops.distinct_until_changed(), )
def _start_periodic_refresh(self) -> None: _LOG.debug("start refresh") self._composite_disposable.add( rx.interval(timedelta(milliseconds=999), scheduler=self._scheduler).pipe( operators.start_with(0), operators.subscribe_on(self._scheduler), operators.observe_on(GtkScheduler(GLib)), ).subscribe(on_next=self._on_periodic_refresh_tick, on_error=lambda e: _LOG.exception( f"Refresh error: {str(e)}")))
async def task(loop): gen = observable_to_async_iterable( Multiply(2) .take(5) .do_action(lambda x: print("observable", x, threading.current_thread())) .to_observable(rx.interval(0.25)), loop, ) async for i in gen: print(i, threading.current_thread()) print("done")
def _start_refresh(self) -> None: _LOG.debug("start refresh") refresh_interval = self._settings_interactor.get_int( 'settings_refresh_interval') self._composite_disposable.add( rx.interval(refresh_interval, scheduler=self._scheduler).pipe( operators.start_with(0), operators.subscribe_on(self._scheduler), operators.flat_map(lambda _: self._get_status()), operators.observe_on(GtkScheduler(GLib)), ).subscribe(on_next=self._update_status, on_error=self._handle_refresh_error))
def _wait_for_order_fill(self, market: Market, client_id: int, max_wait_in_seconds: int = 60): self.logger.info( f"Waiting up to {max_wait_in_seconds} seconds for {client_id}.") return rx.interval(1.0).pipe( ops.flat_map(lambda _: market.load_event_queue()), ops.skip_while(lambda item: item.client_order_id != client_id), ops.skip_while(lambda item: not item.event_flags.fill), ops.first(), ops.map(lambda _: True), ops.timeout(max_wait_in_seconds, rx.return_value(False))).run()
def sequentialSocket(channel): scheduler = rx.scheduler.timeoutscheduler.TimeoutScheduler() stream = rx.interval(SOCKET_LIMIT_SEC-10, scheduler).pipe( ops.start_with('start'), ops.map(lambda _: temporarySocket(channel)), ops.switch_latest(), ops.filter(lambda res: res[:2] == '42'), ops.map(lambda res: json.loads(res[13:-1])), ops.map(Response.from_dict), ops.map(lambda res: res.message.data), ) return stream
async def test_buffered_aio(): sched = AsyncIOScheduler(asyncio.get_running_loop()) events = [] outs = [] @rxpy_backpress.wrap_aio async def block(x): events.append(x) await asyncio.sleep(0.5) return x rx.interval(0.05, scheduler=sched).pipe(rxpy_backpress.BackpressBuffered(), ops.flat_map(block)).subscribe( on_next=outs.append, scheduler=sched) await asyncio.sleep(1.1) assert events == [0, 1, 2] assert outs == [0, 1]
def test_interval(): print('rx.interval(1)') obs = rx.interval(1) input('Press Enter to subscribe...\n') unsub = obs.subscribe(lambda x: print(x)) input('Press Enter to dispose...\n') unsub.dispose() input('Press Enter to subscribe again...\n') unsub = obs.subscribe(lambda x: print(x)) input('Press Enter to finish...\n') unsub.dispose()
def main(): logging.info(f"Starting bot") rx.interval(10).pipe( ops.map(lambda i: i * 100), ops.observe_on(pool_scheduler), ops.do_action(lambda _: set_ids(_)), ops.map(lambda s: intense_calculation(s))).subscribe( on_next=lambda i: logging.info(f"PROCESS 10s: {i}"), on_error=lambda e: logging.error(e)) with concurrent.futures.ProcessPoolExecutor( optimal_thread_count) as executor: while True: rx.from_(get_ids()).pipe( ops.map(lambda i: i * 100), ops.do_action(lambda i: logging.info(f"i is {i}")), # ops.subscribe_on(pool_scheduler), # ops.flat_map(get_ids()), ops.flat_map(lambda s: executor.submit(intense_calculation, s)) # ops.map(lambda s: intense_calculation(s)) ).subscribe(on_next=lambda i: logging.info(f"PROCESS 1s: {i}"), on_error=lambda e: logging.error(e)) input("Press any key to exit\n")
def open(self): print("WebSocket opened") self.write_message("connection opened") def _send_response(x): print(x) self.write_message(json.dumps(x)) def _on_error(ex): print(ex) self.subject = Subject() self.subject.pipe(buffer(rx.interval(5.0)), last(), flat_map(self.get_data)).subscribe( on_next=_send_response, on_error=_on_error)
def test_ref_count(): print('rx.interval(1).pipe(ops.publish(), ops.ref_count())') obs = rx.interval(1).pipe(ops.publish(), ops.ref_count()) input('Press Enter to subscribe...\n') unsub = obs.subscribe(lambda x: print(x)) input('Press Enter to dispose...\n') unsub.dispose() input('Press Enter to subscribe again...\n') unsub = obs.subscribe(lambda x: print(x)) input('Press Enter to finish...\n') unsub.dispose()
def main(): try: client = carla.Client('localhost', 2000) client.set_timeout(2.0) world = client.get_world() blueprint_library = world.get_blueprint_library() bp = random.choice(blueprint_library.filter('vehicle')) rgb_bp = blueprint_library.find('sensor.camera.rgb') spawn_points = world.get_map().get_spawn_points() spawn_point = random.choice(spawn_points) if spawn_points else carla.Transform() vehicle = world.try_spawn_actor(bp, spawn_point) print('created %s' % vehicle.type_id) vehicle.set_autopilot(True) sensors = [make_sensor(world, vehicle, rgb_bp, config[0], config[1]) for config in CAMERA_CONFIGS] image_obs = [from_sensor(sensor, idx) for idx, sensor in enumerate(sensors)] rx.interval(1).pipe( ops.with_latest_from(*image_obs), ).subscribe(grid_draw) time.sleep(5) finally: print('finally') for sensor in sensors: sensor.stop() sensor.destroy() vehicle.destroy() print('done')
def complexMerge(self): # This is infinite source = interval(1).pipe( op.map(lambda item: item * 10), op.map(lambda second: self.intense_calculation(second))) source2 = of('Alpha', 'Beta', 'Gamma', 'Delta', 'Epsilon').pipe( op.map(lambda second: self.intense_calculation(second))) result = source.pipe(op.merge(source2)) result.subscribe(on_next=lambda item: print( '"Subscribe Complex Merge" output: {}'.format(item)), on_error=lambda err: print('Error: {}'.format(e))) input('Press any key to exit\n')
def main(sources): ticks_stream = rx.interval(timedelta(seconds=1)).pipe( map(lambda n: n + 1), take(10)) responses_stream = sources["http"].pipe(flat_map(lambda s: s), ) queries = rx.of( { "url": "https://jsonplaceholder.typicode.com/todos/1" }, { "url": "https://error123123123.co.uk" }, ).pipe(delay(timedelta(seconds=2))) return { "log": rx.merge(queries, responses_stream, ticks_stream), "http": queries }
def timestampOperator(self): source = interval(1).pipe( op.map(lambda second: self.intense_calculation(second)), op.timestamp() ) source2 = of('Alpha', 'Beta', 'Gamma', 'Delta', 'Epsilon').pipe( op.map(lambda second: self.intense_calculation(second)), op.timestamp() ) result = source.pipe( op.merge(source2) ) result.subscribe(lambda item: print('"Subscribe Timestamp" output: {}'.format(item)))
def sample(source: Observable) -> Observable: """Samples the observable sequence at each interval. Examples: >>> res = sample(source) Args: source: Source sequence to sample. Returns: Sampled observable sequence. """ if interval is None: return sample_observable(source, sampler) return sample_observable(source, rx.interval(interval, scheduler=scheduler))
def sample(source: Observable) -> Observable: """Samples the observable sequence at each interval. Examples: >>> res = sample(source) Args: source: Source sequence to sample. Returns: Sampled observable sequence. """ if interval is None: return sample_observable(source, sampler) return sample_observable(source, rx.interval(interval, scheduler=scheduler))
def play_step(self, step, cancel): interval = rx.interval(0.1) interval_steps = rx.just(step).pipe( ops.flat_map(lambda step: interval.pipe(ops.map(lambda _: step)))) step_done = interval_steps.pipe( ops.filter(lambda step: self.player.position() >= step.step_end), ops.do_action( lambda step: self.player.set_position(step.loop_start)), ops.take(1)) loop_done = interval_steps.pipe( ops.filter(lambda step: self.player.position() >= step.loop_end), ops.do_action( lambda step: self.player.set_position(step.loop_start)), ops.take_until(cancel.pipe(ops.skip(1)))) return step_done.pipe(ops.merge(loop_done))
def sample(source: Observable) -> Observable: """Samples the observable sequence at each interval. Examples: >>> res = sample(source) Args: source: Source sequence to sample. Returns: Sampled observable sequence. """ if isinstance(sampler, typing.Observable): return sample_observable(source, sampler) else: return sample_observable(source, rx.interval(sampler, scheduler=scheduler))
def build(self): # Frame rate fps = 10 # clock clock_source = rx.interval(1 / fps) webcam = self.webcam resize_image = lambda image: vision.resize_by_max_width(image, max_width=500) blur = lambda image: filters.gaussian_blur(image, kernel_size=21) pipeline = clock_source.pipe( ops.map( lambda index: Context(index=index, image=next(webcam).frame)), ops.map(step(resize_image, "image", "image")), ops.map(step(vision.to_gray, "image", "gray")), ops.map(step(blur, "gray", "gray")), ops.map(step(self.detector, ["index", "image", "gray"])), ) self.pipeline = pipeline
def projection(x): return rx.interval(10).pipe(ops.map_indexed(lambda a, b: x), ops.take(x))
def create(): return rx.interval(1000)