0

assume you have an Observable stream of values which pushes a values very fast.

Now let's say you have a subscriber which needs to update a record in a database with the latest values from this observable stream, i.e. a somewhat slow I/O bound consumer.

In other words, imagine

from rx import Observable
obs = Observable.interval(1)

# Whoops. Observable will push values faster than we can consume them here.
sub = obs.subscribe( do_some_io_bound_operation )

Is there any way to "skip" all except for the latest values that the observable creates in between different on_next invocations?

In other words: Assume in the example above, obs starts pushing values 1,2,3... The subscriber invokes do_some_io_bound_operation with value "1". This takes a while - by the time its done, values 2 and 3 are available. But instead of calling do_some_io_bound_operation on both new available values, the subscriber should ideally just skip value "2" and directly move on the value 3.

Bit hard to describe - I hope the intention is clear. Is there any way to achieve this?

I'd suppose .Buffer() goes into the direction - but most applications I've seen just buffer by some fixed number of elements or a timespan, whereas I'd need to buffer dynamically (buffer everything that happens while on_next is being executed)

Thanks

Bogey
  • 4,926
  • 4
  • 32
  • 57

1 Answers1

1

No, there is no right way.
The Observable notifies the subscribed Observer instance whenever an event occurs, so the Subscriber does not have access to all items and can not affect the stream generation.
You can perform time-based operations on Observable level, debounce for example, but you cannot block/skip elements during the work on the Subscriber.
You can implement your own custom operator for skipping messages but this is idiomatically wrong.

Roman Patutin
  • 2,171
  • 4
  • 23
  • 27