async def test_forward_pipe_complex_pipe() -> None: xs = AsyncObservable.from_iterable([1, 2, 3]) result = [] def mapper(value) -> int: return value * 10 async def predicate(value) -> bool: await asyncio.sleep(0.1) return value > 1 async def long_running(value) -> AsyncObservable[int]: return AsyncObservable.from_iterable([value]) ys = (xs | _.filter(predicate) | _.map(mapper) | _.flat_map(long_running) | _.to_async_iterable() ) async for value in ys: result.append(value) assert result == [20, 30]
async def main() -> None: xs = AsyncObservable.from_iterable([1, 2, 3, 4, 5]) async def mapper(value) -> AsyncObservable: fut = executor.submit(long_running, value) return AsyncObservable.unit(wrap_future(fut)) ys = xs | op.flat_map(mapper) | op.to_async_iterable() async for x in ys: print(x)
async def init(self): await self.connection.connect() unpack = itemgetter("config", "old_val", "new_val") async def update(x): cfg, old, new = unpack(x) try: if new is None: self.latest_config[cfg].delete(old) else: self.latest_config[cfg].set(new) except: logger.exception(f"Unhandled error while updating config {cfg}.") else: logger.info(f"Updated config {cfg} with {x}") return await subscribe( from_iterable(self.configs.values()) | Operators.flat_map(self.config_observable), AsyncAnonymousObserver(update))
async def test_pipe_complex_pipe(): xs = AsyncObservable.from_iterable([1, 2, 3]) result = [] def mapper(value): return value * 10 async def predicate(value): await asyncio.sleep(0.1) return value > 1 async def long_running(value): return AsyncObservable.from_iterable([value]) ys = pipe(xs, _.filter(predicate), _.map(mapper), _.flat_map(long_running), _.to_async_iterable()) async for value in ys: result.append(value) assert result == [20, 30]