官术网_书友最值得收藏!

The interval operator

In cases in which some items must be emitted at a fixed interval, the interval operator can be used instead of the timer operator. Its behavior is the same as the timer operator, wherein the same value is provided in duetime, a period parameter. This is how it is implemented. Its marble diagram is shown in the following figure:

Figure 4.9: The interval operator

Its prototype is as follows:

Observable.interval(period, scheduler=None)

The period parameter is the value, in milliseconds, of the period of item emission. This operator can be used as follows:

import datetime

ticks = Observable.interval(1000)
ticks.subscribe(
on_next=lambda i: print("tick {} at {}".format(
i, datetime.datetime.now())),
on_error=lambda e: print("error: {}".format(e)),
on_completed=lambda: print("completed")
)

The preceding example provides the same results as the second example of the timer operator, as follows:

tick 0 at 2018-06-23 22:07:32.488799
tick 1 at 2018-06-23 22:07:33.491213
tick 2 at 2018-06-23 22:07:34.493068
tick 3 at 2018-06-23 22:07:35.495030
tick 4 at 2018-06-23 22:07:36.500543
...
主站蜘蛛池模板: 贵港市| 尚义县| 营山县| 民县| 四会市| 闽清县| 潍坊市| 凌云县| 和龙市| 高唐县| 金华市| 惠安县| 隆子县| 揭西县| 澳门| 睢宁县| 华宁县| 横峰县| 阳高县| 增城市| 杭州市| 崇义县| 长乐市| 乐都县| 新津县| 咸丰县| 朔州市| 灵山县| 桃园市| 宜兰市| 泽普县| 巍山| 宁津县| 东乡族自治县| 新昌县| 杭州市| 三河市| 西平县| 伊宁县| 息烽县| 布尔津县|