-1

There's a source that emits items asynchronously, in my case in the order of 10 items withing some seconds.

  • I want to handle all of them.
  • In order of appearance
  • One at a time
  • Handling of each might take long (like 1-5 seconds)
  • There might be new items while one is being handled

At the moment I'm porting a Java app to C#. In the RxJava world there's the onBackpressureBuffer method, whereas in Rx.Net there are a bunch of different ones and cannot figure out the equivalent.

I guess I could use Buffer() with a zero delay and use the produced enumerable, but seems to be hacky.

edit: please read comments before voting negatively

Odys
  • 8,951
  • 10
  • 69
  • 111
  • I thought in c# if you use fromasync or async toobservable then unless you use ObserveOn you will be handling sequentially on a single thread anyway? – Sentinel Jan 27 '18 at 21:15
  • @Sentinel I think that's how RX works in general. Assume though that there are two threads. One produces and one consumes and the later takes time to do it. I don't want the later to say "stop sending me, I cannot handle all of it" but rather say "will process that also, just wait". – Odys Jan 28 '18 at 22:08
  • I am lost. Why don't you just ObserveOn and handle everything in parallel? – Sentinel Jan 29 '18 at 00:15
  • Or if, as in another recent question, you want a prioritized queue, you can use Scan to build a list of things to process – Sentinel Jan 29 '18 at 00:19
  • Actually, it's not clear from your question what you want at all – Sentinel Jan 29 '18 at 00:19
  • @Sentinel I am porting code from Java to C#. Using React in both, I'd expect a method or methodology match between the two, especially since they share so much in architecture level (CLR/JVM, common threading model, and the languages are almost twins). Of course there are differences, primitives and LINQ most notably, but makes little sense to me having similar logic with completely different names. `Scan` for example looks promising but didn't had the time to test it. I hope my question is much clearer now. – Odys Feb 02 '18 at 16:05
  • 1
    Downvoted because the critical requirement has been omitted from the question: you want to block the fast producer from emitting items while the slow consumer has its buffer full of unprocessed items. This requirement cannot be fulfilled without bidirectional communication between the producer and the consumer, which goes against the observable/observer paradigm. Hence there is no support for it out of the box in Rx. – Theodor Zoulias Dec 13 '20 at 11:19

1 Answers1

3

Rx.NET doesn't support backpressure and it can't the way RxJava does: one has to design the protocol so that there is a request channel between a producer and its consumer. In concept, Async Enumerables can get you backpressure in the form of 1-by-1 item delivery (called async pull, promise per item, continuations, etc. in some cases).

There is a C# library that matches the features of the RxJava Flowable type relatively well (but not 100%): Reactive4.NET that can also interop with IObservable and thus with Rx.NET if needed.

akarnokd
  • 69,132
  • 14
  • 157
  • 192