geronimo-iia / async-rx

python async react library
https://geronimo-iia.github.io/async-rx/
Other
4 stars 0 forks source link

How do you use `rx_debounce`? #7

Open garyvdm opened 3 years ago

garyvdm commented 3 years ago

This is more a question than a bug.

I can't workout how to use rx_debounce. I think this is the closest I got with all my tries:

@rx_observer
async def my_observer(item):
    print(item)

source = rx_range(0, 5)
debounced = rx_debounce(source, timedelta(seconds=1))
await debounced.subscribe(my_observer)

File "rx-test.py", line 101, in main
    await debounced.subscribe(my_observer)
File "ve/lib/python3.9/site-packages/async_rx/protocol/observable.py", line 37, in _subscribe
    return await subscribe(ensure_observable_contract_operator(an_observer))
File "ve/lib/python3.9/site-packages/async_rx/observable/rx_create.py", line 43, in _subscribe_tracked
    subscription = await subscribe(an_observer)
File "ve/lib/python3.9/site-packages/async_rx/observable/rx_debounce.py", line 83, in _subscribe
    _consumer_task = await curio.spawn(consumer())
File "ve/lib/python3.9/site-packages/curio/task.py", line 613, in spawn
    task = await _spawn(coro)
File "ve/lib/python3.9/site-packages/curio/traps.py", line 83, in _spawn
    return await _kernel_trap('trap_spawn', coro)
File "ve/lib/python3.9/site-packages/curio/traps.py", line 32, in _kernel_trap
    result = yield request
RuntimeError: Task got bad yield: ('trap_spawn', <coroutine object rx_debounce.<locals>._subscribe.<locals>.consumer at 0x7f76ce9f5ac0>)
sys:1: RuntimeWarning: coroutine 'rx_debounce.<locals>._subscribe.<locals>.consumer' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
geronimo-iia commented 3 years ago

Hello,

rx_observer is not an annotation (maybe i should do one by the way).

I havent my laptop, but something like this seem to me more appropriate:

observer = rx_observer(on_next= my_observer) source = rx_range(0, 5) debounced = rx_debounce(source, timedelta(seconds=1)) await debounced.subscribe(observer) await curio.sleep(6)

garyvdm commented 3 years ago

It seems to be an issue when running with the stdlib asyncio eventloop. When I run with curio it works. Not sure is there is something that can be done to make this work. I will investigate further.

P.s rx_observer perfectly as a decorator if you only have an on_next method.

geronimo-iia commented 3 years ago

Hello, this module use curio as underlaying implementation, I ever read few doc about the use of std asyncio and curio but never yet tested.