The timestamp operator

The timestamp operator adds a timestamp to each item emitted. The following figure shows the marble diagram of this operator:

Figure 9.23: The timestamp operator

Its prototype is the following:

Observable.timestamp(self, scheduler=None)

An alternative scheduler can be provided with the scheduler parameter. If no scheduler parameter is provided, then the timeout scheduler is used.

Here is an example of the timestamp scheduler:

numbers = Subject()

numbers.timestamp().subscribe(
on_next = lambda i: print("on_next {}: {}".format(i.value, i.timestamp)),
on_error = lambda e: print("on_error: {}".format(e)),
on_completed = lambda: print("on_completed")
)

numbers.on_next(1)
time.sleep(0.1)
numbers.on_next(2)
time.sleep(0.1)
numbers.on_next(3)
time.sleep(0.1)
numbers.on_next(4)

This gives the following result:

on_next 1: 2018-08-15 21:53:18.642451
on_next 2: 2018-08-15 21:53:18.746261
on_next 3: 2018-08-15 21:53:18.847760
on_next 4: 2018-08-15 21:53:18.948793

The four items are received with a delay of 100 milliseconds between each.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.158.134