如何在长时间运行的查询中延长油门时间跨度? [英] How to extend Throttle Timespan in middle of a long running query?

查看:36
本文介绍了如何在长时间运行的查询中延长油门时间跨度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在查询中间扩展 Throttle Timespan 值?例如,假设在 101 Rx Samples Throttle 中的示例有这个查询= observable.Throttle(TimeSpan.FromMilliseconds(750));.

Is it possible to extend Throttle Timespan value in middle of a query? For instance, assuming an example as in 101 Rx Samples Throttle there is this query var throttled = observable.Throttle(TimeSpan.FromMilliseconds(750));.

如果我想改变它,如果在前 500 毫秒内没有事件,那么限制值将扩展到,例如,之后每个事件的 1500 毫秒.

What if I wanted to change it so that if during the first 500 ms there will be no events, then the throttling value would be extended to, e.g., 1500 ms for every event thereafter.

这是一个使用 Switch 操作符的地方吗?

Would this be a place to use the Switch operator?

推荐答案

Throttle 有一个重载,它接受一个工厂函数,该函数接受源事件并产生一个节流流",它是一个IObservable(T 可以是任何类型).事件将被抑制,直到油门流发出.

There is an overload of Throttle that accepts a factory function that takes the source event and produces a "throttle stream" that is an IObservable<T> (T can be any type). Events will be suppressed until the throttle stream emits.

下面的例子有一个每秒泵送的流,一个油门工厂生产一个 0.5 秒的油门.因此,在开始时,源流不会受到限制.

The following example has a stream that pumps every second, with a throttle factory producing a 0.5 second throttle. Thus at start, the source stream is not throttled.

如果您输入例如 2,则油门将更改为两秒油门,并且所有事件都将被抑制.向下更改为 1,事件将再次出现.

If you enter say, 2, the throttle will change to a two second throttle and all events will be suppressed. Change down to 1 and events will appear again.

void Main()
{
    var throttleDuration = TimeSpan.FromSeconds(0.5);
    Func<long, IObservable<long>> throttleFactory =
        _ => Observable.Timer(throttleDuration);

    var sequence = Observable.Interval(TimeSpan.FromSeconds(1))
                             .Throttle(throttleFactory);

    var subscription = sequence.Subscribe(Console.WriteLine);

    string input = null;
    Console.WriteLine("Enter throttle duration in seconds or q to quit");
    while(input != "q")
    {       
        input = Console.ReadLine().Trim().ToLowerInvariant();
        double duration;

        if(input == "q") break;
        if(!double.TryParse(input, out duration))
        {
            Console.WriteLine("Eh?");
            continue;
        }
        throttleDuration = TimeSpan.FromSeconds(duration);
    }

    subscription.Dispose();
    Console.WriteLine("Done");
}

因为这是一个为每个事件生成油门的工厂函数,所以您可以创建更动态的东西,根据特定的输入事件返回油门流.

Because this is a factory function producing the throttle per event, you can create something much more dynamic that returns a throttle stream based on the particular input event.

像这样使用流作为控件的想法是在整个 Rx API 中使用的一种非常常见的技术,非常值得您思考:类似用法的示例包括 other 参数 othercode>TakeUntil,GroupByUntil 中的 durationSelectorBuffer 中的 bufferClosingSelector.

The idea of a stream used as a control like this is a very common technique used throughout the Rx API and is well worth wrapping your head around: examples of similar uses include the other argument to TakeUntil, the durationSelector in GroupByUntil, the bufferClosingSelector in Buffer.

这篇关于如何在长时间运行的查询中延长油门时间跨度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆