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

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
...
主站蜘蛛池模板: 清苑县| 吉隆县| 双桥区| 罗田县| 介休市| 湄潭县| 萍乡市| 万山特区| 成都市| 工布江达县| 汶川县| 滦南县| 河西区| 海盐县| 航空| 宕昌县| 奉节县| 徐汇区| 上犹县| 辛集市| 横山县| 眉山市| 鄱阳县| 曲靖市| 石柱| 娄底市| 静乐县| 曲水县| 铜山县| 大冶市| 临沂市| 永城市| 城步| 湟中县| 和林格尔县| 若尔盖县| 尼玛县| 桑日县| 广丰县| 吴桥县| 昌江|