async def main(): xs = AsyncObservable.from_iterable(range(10)) # Split into odds and evens odds = xs | op.filter(lambda x: x % 2 == 1) evens = xs | op.filter(lambda x: x % 2 == 0) async def mysink(value): print(value) await subscribe(odds, AsyncAnonymousObserver(mysink)) await subscribe(evens, AsyncAnonymousObserver(mysink))
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 test_forward_pipe_simple_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 ys = xs | _.filter(predicate) | _.map(mapper) async def asend(value) -> None: result.append(value) await run(ys, AsyncAnonymousObserver(asend)) assert result == [20, 30]
async def test_pipe_simple_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 ys = pipe(xs, _.filter(predicate), _.map(mapper)) async def asend(value): result.append(value) await run(ys, AsyncAnonymousObserver(asend)) assert result == [20, 30]
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]
async def new_task_watch(): db_host = os.environ.get('DATABASE_HOST') db_port = os.environ.get('DATABASE_PORT') conn = await connection(db_host, db_port) async def new_change(arg): try: row, webhooks = arg[0], arg[1] logger.debug(f"Dispatching row: {row}") event = row.get('event', 'undefined') hooks = webhooks.get(event, []) if len(hooks) > 0: webhook_future = asyncio.ensure_future( service.webhook(hooks, row)) try: handler = dispatch[event] await handler(conn, row) except KeyError: logger.debug(f"No handler for event type {row['event']}") except: logger.exception("Unknown exception in task processing") try: if len(hooks) > 0: await webhook_future except: logger.exception("Unknown exception in webhook.") except: logger.exception("Unknown exception in task watch.") async def delayed(tasks): logger.debug(f"Got update in delayed tasks: {tasks}") if len(tasks) == 0: await asyncio.sleep(60) return task = next(iter(tasks)) delta = datetime.fromtimestamp( task['at'], timezone.utc) - datetime.now(timezone.utc) await asyncio.sleep(delta.total_seconds()) await conn.run(new_task(conn, task['event'], task['parameters'])) await asyncio.shield(conn.run(remove_delayed(conn, task))) ready_tasks = conn.start_with_and_changes(conn.db().table('tasks') \ .filter(lambda row: row['status'] == 'ready') ) | Operators.map(lambda c: c['new_val'])\ | Operators.filter(lambda x: x is not None) webhooks = conn.changes_accumulate(conn.db().table('webhooks'))\ | Operators.map(group_by("event")) return await asyncio.gather( subscribe( ready_tasks | with_latest_from(webhooks), AsyncAnonymousObserver(new_change) ), subscribe( conn.changes_accumulate(conn.db().table('delayed_tasks'))\ | Operators.map(lambda tasks: sorted(tasks, key=lambda task: task['at'])), AsyncRepeatedlyCallWithLatest(delayed) ) )