Parallelism notification in observable

I have the following observable

IObservable<Work> observable = SomeMethodToGetWorkToDo();

Each time it is called OnNext, I have to do the work in a separate thread. It takes a lot of time to complete each work, so I can’t have other items Workin the queue, for so long I have enough system resources.

I thought that ObserveOnwould solve my problem, but when I launched several calls Console.WriteLineto see the thread IDs, I saw the same thread ID for each notification call.

How can I be sure of parallelism in OnNext?

+4
source share
2 answers

", " "", . - TPL ( ). - :

observable
    .SelectMany(work => Task.Run(() => DoWork(work))
    .Subscribe(workResult => Console.WriteLine("a work item was completed"));
+5

ObserveOn :

var sample = Observable.Interval(TimeSpan.FromMilliseconds(100));

sample
    .SelectMany(l => Observable.Return(l)
        .ObserveOn(TaskPoolScheduler.Default /*or NewThreadScheduler.Default */)
    )
    .Select(l => Thread.CurrentThread.ManagedThreadId)
    .Take(9)
    .Subscribe(i => Console.WriteLine(i));

, , .

+3

Source: https://habr.com/ru/post/1676183/


All Articles