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

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
...
主站蜘蛛池模板: 徐州市| 沐川县| 东台市| 招远市| 沂水县| 宝清县| 甘泉县| 邵阳市| 桃园县| 盘山县| 廉江市| 资中县| 澎湖县| 永康市| 大埔县| 霍邱县| 定襄县| 彭泽县| 云和县| 林甸县| 板桥市| 陇西县| 泽普县| 治多县| 琼中| 白朗县| 古蔺县| 莒南县| 准格尔旗| 金寨县| 博爱县| 秭归县| 淮南市| 华亭县| 集贤县| 祁连县| 阜宁县| 富民县| 沛县| 吴川市| 米脂县|