async def project_stop_impl(subject: ReplaySubject, project_name: str, services: List[str]): try: project = load_single_project(project_name).config except GraphQLError as ex: subject.on_next(StartStopEndStep( error_string=str(ex), is_fatal_error=True )) subject.on_completed() return engine = registry().engine if len(services) < 1: services = project["app"]["services"].keys() last_steps: Dict[str, int] = {} try: async for service_name, status, finished in engine.stop_project(project, services): _handle_update(finished, last_steps, service_name, status, subject, "Service stopped!") except Exception as err: print(traceback.format_exc()) subject.on_next(StartStopEndStep( error_string="Error stopping the services: " + str(err), is_fatal_error=True )) else: subject.on_next(StartStopEndStep()) subject.on_completed()
def rx_subscribe(node, data_class=String, parse=unpickle, buffer_size=1): source = ReplaySubject(buffer_size) rospy.Subscriber( node, data_class, lambda msg: source.on_next(parse(msg) if parse is not None else msg), queue_size=buffer_size) return source.as_observable()
def calc_gen(self, gen): fitnessless = gen.get_fitlessness() if len(fitnessless) == 0: return tick_tack = ReplaySubject() fset = seq(fitnessless) \ .map(lambda wchr: (wchr['name'], wchr['id'])) \ .to_set() results = [] def remove_fset(result): wchr, fit = result tp = wchr['name'], wchr['id'] if tp in fset: fset.discard(tp) results.append(result) if len(fset) == 0: tick_tack.on_next(0) s1 = self.executor.sub_compl.subscribe(on_next=remove_fset) for fn in fitnessless: self.executor.sub_in.on_next(fn) if len(fset) == 0: tick_tack.on_next(0) tick_tack.to_blocking().first() tick_tack.dispose() s1.dispose() gen.init_fitnesses(results) if len(gen.get_fitlessness()) > 0: raise AssertionError("Посчитались не все гены в хромосоме")
async def db_new_impl(subject: ReplaySubject, project_name: str, new_name: str, switch: bool): project = try_loading_project(project_name, subject, 1, 4) if not project: return engine = registry().engine dbenv = DbEnvironments(project, engine) try: subject.on_next(ResultStep( steps=4, current_step=1, text=f"Creating database environment {new_name}..." )) dbenv.new(new_name, copy_from=None) except FileExistsError: subject.on_next(ResultStep( steps=4, current_step=1, text=f"Environment with this name already exists.", is_end=True, is_error=True )) except NameError: subject.on_next(ResultStep( steps=4, current_step=1, text=f"Invalid name for new environment, do not use special characters.", is_end=True, is_error=True )) except Exception as ex: subject.on_next(ResultStep( steps=4, current_step=1, text=f"Error creating environment: {ex}", is_end=True, is_error=True )) else: if switch: await db_switch_impl(subject, project, new_name, 1, 1) else: subject.on_next(ResultStep( steps=4, current_step=4, text=f"Finished creating environment: {new_name}.", is_end=True ))
def resolve_project_stop(parent, info, project_name: str, services=None): if services is None: services = [] subject = ReplaySubject() asyncio.get_event_loop().run_in_executor(None, project_stop_impl, subject, project_name, services) return subject
async def db_drop_impl(subject: ReplaySubject, project_name: str, name: str): project = try_loading_project(project_name, subject, 1, 4) if not project: return engine = registry().engine dbenv = DbEnvironments(project, engine) try: subject.on_next(ResultStep( steps=1, current_step=1, text=f"Deleting database environment {name}..." )) dbenv.drop(name) except FileNotFoundError: subject.on_next(ResultStep( steps=1, current_step=1, text=f"Environment with this name does not exist.", is_end=True, is_error=True )) except OSError: subject.on_next(ResultStep( steps=1, current_step=1, text=f"Can not delete the environment that is currently active.", is_end=True, is_error=True )) except Exception as ex: subject.on_next(ResultStep( steps=1, current_step=1, text=f"Error deleting environment: {ex}", is_end=True, is_error=True )) else: subject.on_next(ResultStep( steps=1, current_step=1, text=f"Finished deleting environment: {name}.", is_end=True ))
def resolve_project_db_new(parent, info, project_name: str, new_name: str, switch=True): subject = ReplaySubject() asyncio.get_event_loop().run_in_executor(None, db_new_impl, subject, project_name, new_name, switch) return subject
def resolve_project_db_copy(parent, info, project_name: str, source: str, target: str, switch=True): subject = ReplaySubject() asyncio.get_event_loop().run_in_executor(None, db_copy_impl, subject, project_name, source, target, switch) return subject
async def update_images_impl(subject: ReplaySubject, project_name: str): subject.on_next(ResultStep( steps=1, current_step=1, text="Starting image update..." )) project = try_loading_project(project_name, subject, 1, 1) if not project: return try: registry().engine.pull_images(project, line_reset="", update_func=lambda msg: subject.on_next( ResultStep( steps=1, current_step=1, text=msg ) )) subject.on_next(ResultStep( steps=1, current_step=1, text="Done updating images!", is_end=True )) except Exception as ex: subject.on_next(ResultStep( steps=1, current_step=1, text="Error updating an image: " + str(ex), is_end=True, is_error=True )) finally: subject.on_completed() pass
def _replay( mapper: Optional[Mapper] = None, buffer_size: Optional[int] = None, window: Optional[typing.RelativeTime] = None, scheduler: Optional[Scheduler] = None ) -> Callable[[Observable], Union[Observable, ConnectableObservable]]: """Returns an observable sequence that is the result of invoking the mapper on a connectable observable sequence that shares a single subscription to the underlying sequence replaying notifications subject to a maximum time length for the replay buffer. This operator is a specialization of Multicast using a ReplaySubject. Examples: >>> res = replay(buffer_size=3) >>> res = replay(buffer_size=3, window=500) >>> res = replay(None, 3, 500) >>> res = replay(lambda x: x.take(6).repeat(), 3, 500) Args: mapper: [Optional] Selector function which can use the multicasted source sequence as many times as needed, without causing multiple subscriptions to the source sequence. Subscribers to the given source will receive all the notifications of the source subject to the specified replay buffer trimming policy. buffer_size: [Optional] Maximum element count of the replay buffer. window: [Optional] Maximum time length of the replay buffer. scheduler: [Optional] Scheduler the observers are invoked on. Returns: An observable sequence that contains the elements of a sequence produced by multicasting the source sequence within a mapper function. """ if mapper: def subject_factory(scheduler): return ReplaySubject(buffer_size, window, scheduler) return ops.multicast(subject_factory=subject_factory, mapper=mapper) return ops.multicast(ReplaySubject(buffer_size, window, scheduler))
def __init__(self, chr_contr, executor, gen0=None, opts=default_options): self.gen0 = gen0 self.chr_contr = chr_contr self.pauser = Pauser() self.executor = executor self.executor.kpp_fun = self.pauser.kpp self.sub_chr_best = ReplaySubject() self.sub_gen_solved = Subject() self.sub_gen_unsolved = Subject() self.subj_done = Subject() self.su_genetic = StepUpGenetic(chr_contr) self.opts = dict(default_options) self.init_opts(opts) self.stop_flag = True self.gen_flag = True self.init_logic()
def replay( source: ObservableBase, mapper: Mapper = None, buffer_size: int = None, window: timedelta = None, scheduler: Scheduler = None ) -> Union[ObservableBase, ConnectableObservable]: """Returns an observable sequence that is the result of invoking the mapper on a connectable observable sequence that shares a single subscription to the underlying sequence replaying notifications subject to a maximum time length for the replay buffer. This operator is a specialization of Multicast using a ReplaySubject. Example: res = source.replay(buffer_size=3) res = source.replay(buffer_size=3, window=500) res = source.replay(None, 3, 500) res = source.replay(lambda x: x.take(6).repeat(), 3, 500) Keyword arguments: mapper -- [Optional] Selector function which can use the multicasted source sequence as many times as needed, without causing multiple subscriptions to the source sequence. Subscribers to the given source will receive all the notifications of the source subject to the specified replay buffer trimming policy. buffer_size -- [Optional] Maximum element count of the replay buffer. window -- [Optional] Maximum time length of the replay buffer. Returns an observable sequence that contains the elements of a sequence produced by multicasting the source sequence within a mapper function. """ if mapper: def subject_factory(scheduler): return ReplaySubject(buffer_size, window, scheduler) return source.multicast(subject_factory=subject_factory, mapper=mapper) return source.multicast(ReplaySubject(buffer_size, window, scheduler))
def __init__(self): self.playing = False self.current_position = 0 self.current_duration = 1 self.current_state = self.State.paused self.state = ReplaySubject(1) self.current_track = ReplaySubject(1) self.position = ReplaySubject(1) self.duration = ReplaySubject(1) self.end_of_track = Subject() self.end_of_track.subscribe(lambda _: logger.debug('END OF TRACK')) self.state.subscribe(self.set_state) self.position.subscribe(self.set_position) self.duration.subscribe(self.set_duration) self.time_tracking = self.position.pipe(map(self.make_time_tracking))
def __init__(self, app, interval=1800, buffer_size=20): self.app = app self.is_running = False self.interval = interval self.logger = logging.getLogger("AutomatonRunner") self.pool = ThreadPoolExecutor() self.logging_queue = queue.Queue(-1) self.logger.addHandler(QueueHandler(self.logging_queue)) pool_scheduler = AsyncIOScheduler() self.log_source = ReplaySubject(buffer_size=buffer_size, scheduler=pool_scheduler) logging_handler = LoggingRxHandler(self.log_source) logging_handler.setFormatter( logging.Formatter( '\033[34m%(asctime)s \033[91m%(name)s\033[0m %(message)s')) self.logging_queue_listener = QueueListener(self.logging_queue, logging_handler) self.logging_queue_listener.start()
def replay(self, selector, buffer_size=None, window=None, scheduler=None): """Returns an observable sequence that is the result of invoking the selector on a connectable observable sequence that shares a single subscription to the underlying sequence replaying notifications subject to a maximum time length for the replay buffer. This operator is a specialization of Multicast using a ReplaySubject. Example: res = source.replay(buffer_size=3) res = source.replay(buffer_size=3, window=500) res = source.replay(None, 3, 500, scheduler) res = source.replay(lambda x: x.take(6).repeat(), 3, 500, scheduler) Keyword arguments: selector -- [Optional] Selector function which can use the multicasted source sequence as many times as needed, without causing multiple subscriptions to the source sequence. Subscribers to the given source will receive all the notifications of the source subject to the specified replay buffer trimming policy. buffer_size -- [Optional] Maximum element count of the replay buffer. window -- [Optional] Maximum time length of the replay buffer. scheduler -- [Optional] Scheduler where connected observers within the selector function will be invoked on. Returns {Observable} An observable sequence that contains the elements of a sequence produced by multicasting the source sequence within a selector function. """ if callable(selector): def subject_selector(): return ReplaySubject(buffer_size, window, scheduler) return self.multicast(subject_selector=subject_selector, selector=selector) else: return self.multicast(ReplaySubject(buffer_size, window, scheduler))
def wait_compl_ids(self, ids): ids = set(ids) tick_tack = ReplaySubject() def onn(id): ids.discard(id) if len(ids) == 0: tick_tack.on_completed() s3 = self.compl_task_id_subject.subscribe(on_next=onn) for i in self.compl_tasks_ids: ids.discard(i) if len(ids) == 0: tick_tack.on_next(0) tick_tack.to_blocking().first_or_default(0) s3.dispose()
from rx.subjects import ReplaySubject, Subject DIRNAME = os.path.dirname(__file__) def get_config(): config_file = open(os.path.join(DIRNAME, '..', 'config.json'), 'r') config = json.load(config_file) config_file.close() return config CONFIG = get_config() VARS = { 'endpoint_id': None, 'message_queue': ReplaySubject(), 'message_id': 0, 'sock': None, 'message_buffer': [] } EVENT_SOURCE = Subject() def get_message_id(): """get unique message id""" num = VARS['message_id'] VARS['message_id'] = VARS['message_id'] + 1 return '{!s}:{!s}'.format(VARS['endpoint_id'], num) def _on_message(msg):
def action1(scheduler, state=None): subject[0] = ReplaySubject(sys.maxsize, 100, scheduler)
def action1(scheduler, state=None): subject[0] = ReplaySubject(3, 100, scheduler)
def ib_bars_import(hint_date, hint_sym, bar_size="1 min", verbose=False): if bar_size == 5: bar_size=='5 mins' """ Hint_date => YYYYMMD format """ # Generate Event to wait for e = threading.Event() # Generate Observable for results retObservable = ReplaySubject() # Connect To IB global CONN, TICKER_ID conn = CONN # Register input processing function def processInput(x): tz = tzlocal.get_localzone() if isinstance(x, conn.messageTypes['historicalData']): if x.date.startswith("finished"): retObservable.on_completed() else: nextValue = dict([(k, getattr(x, k)) for k in x.keys() if k != "reqId"]) nextValue['date'] = tz.localize(datetime.strptime(nextValue['date'], '%Y%m%d %H:%M:%S')) nextValue['date'] = nextValue['date'].astimezone(pytz.utc).replace(tzinfo=None) retObservable.on_next(nextValue) elif isinstance(x, conn.messageTypes['error']): if "data farm connection is OK" in x.errorMsg: return print (x, file=sys.stderr) if x.errorCode != None: retObservable.on_error(Exception(x.errorMsg)); elif verbose: print (x) # Register it and connect conn.registerAll(processInput) # Request data from server endtime = "%s 17:20:00" % hint_date conn.reqHistoricalData( tickerId=TICKER_ID, contract=make_contract(hint_sym), endDateTime=endtime, durationStr='1800 S', barSizeSetting=bar_size, whatToShow='TRADES', useRTH=1, formatDate=1) TICKER_ID += 1 def on_complete(err=None): e.set() # Register on observable to hear "Complete" event. retObservable.subscribe(None, on_complete, on_complete) # Wait until data completed e.wait() return retObservable.as_observable()
def action1(scheduler, state=None): nonlocal subject subject = ReplaySubject(sys.maxsize, 100, scheduler)
def action1(scheduler, state=None): nonlocal subject subject = ReplaySubject(scheduler=scheduler)
class AudioBackend: class State(Enum): paused = 1 playing = 2 def __init__(self): self.playing = False self.current_position = 0 self.current_duration = 1 self.current_state = self.State.paused self.state = ReplaySubject(1) self.current_track = ReplaySubject(1) self.position = ReplaySubject(1) self.duration = ReplaySubject(1) self.end_of_track = Subject() self.end_of_track.subscribe(lambda _: logger.debug('END OF TRACK')) self.state.subscribe(self.set_state) self.position.subscribe(self.set_position) self.duration.subscribe(self.set_duration) self.time_tracking = self.position.pipe(map(self.make_time_tracking)) def destroy(self) -> None: raise NotImplementedError def set_track(self, track: Track) -> None: raise NotImplementedError def seek(self, position: int) -> None: raise NotImplementedError def play(self) -> None: raise NotImplementedError def pause(self) -> None: raise NotImplementedError def make_time_tracking(self, position: int): return TimeTrack( elapsed=utils.format_seconds(position), total=utils.format_seconds(self.current_duration), progress_percentage=position / self.current_duration, ) def set_position(self, position: int): self.current_position = position def set_duration(self, duration: int): self.current_duration = duration def set_state(self, state): self.current_state = state def play_track(self, track: Track): self.current_track.on_next(track) self.set_track(track) self.play() def rewind_by_seconds(self, offset: int): new_position = self.current_position + offset new_position = min(self.current_duration, max(0, new_position)) self.seek(new_position) def rewind_by_percentage(self, offset: float): self.rewind_by_seconds(self.current_duration * offset) def toggle_pause(self): if self.current_state == self.State.playing: self.pause() elif self.current_state == self.State.paused: self.play() else: logger.error(f'Unknown playback state "{self.current_state}"')
async def update_repositories_impl(subject: ReplaySubject): subject.on_next(ResultStep( steps=2, current_step=1, text="Starting repository update..." )) try: repositories.update(registry().system_config, lambda msg: subject.on_next(ResultStep( steps=2, current_step=1, text=msg ))) except Exception as e: subject.on_next(ResultStep( steps=2, current_step=1, text="Fatal error during repository update: " + str(e), is_end=True, is_error=True )) subject.on_completed() return subject.on_next(ResultStep( steps=2, current_step=2, text="Reloading configuration..." )) # Reload system config try: config_path = riptide_main_config_file() system_config = Config.from_yaml(config_path) system_config.validate() registry.system_config = system_config except FileNotFoundError as e: subject.on_next(ResultStep( steps=2, current_step=2, text="Main config file not found! Could not reload system config.", is_end=True, is_error=True )) except Exception as e: subject.on_next(ResultStep( steps=2, current_step=2, text="Could not reload system config: " + str(e), is_end=True, is_error=True )) else: subject.on_next(ResultStep( steps=2, current_step=2, text="Repository update done!", is_end=True )) finally: subject.on_completed()
def resolve_project_db_drop(parent, info, project_name: str, name: str): subject = ReplaySubject() asyncio.get_event_loop().run_in_executor(None, db_drop_impl, subject, project_name, name) return subject
def make_request_proxies(drivers): requestProxies = {} for name in drivers.keys(): requestProxies[name] = ReplaySubject(1) return requestProxies
def subject_factory(scheduler): return ReplaySubject(buffer_size, window, scheduler)
def resolve_update_images(parent, info, project_name: str): subject = ReplaySubject() asyncio.get_event_loop().run_in_executor(None, update_images_impl, subject, project_name) return subject
def resolve_update_repositories(parent, info): subject = ReplaySubject() asyncio.get_event_loop().run_in_executor(None, update_repositories_impl, subject) return subject
def subject_selector(): return ReplaySubject(buffer_size, window, scheduler)