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

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
...
主站蜘蛛池模板: 丁青县| 柏乡县| 许昌市| 土默特左旗| 建瓯市| 塔城市| 久治县| 江津市| 崇仁县| 扎赉特旗| 丹凤县| 罗甸县| 广水市| 长岭县| 嵩明县| 济阳县| 小金县| 三河市| 平江县| 镇宁| 甘洛县| 襄樊市| 新宾| 桦川县| 长垣县| 铜川市| 鄢陵县| 舞阳县| 蒙阴县| 清新县| 于田县| 台湾省| 秦皇岛市| 濮阳市| 济宁市| 乳源| 阳泉市| 肃北| 台北县| 武冈市| 扬中市|