The delay operator

The delay operator delays the emission of the source observable items by a specified time. The following figure shows the marble diagram of this operator:

Figure 9.17: The delay operator

Its prototype is the following:

Observable.delay(self, duetime, scheduler=None)

Here, the duetime parameter is the delay to apply in milliseconds. The scheduler parameter can be used to specify a scheduler to use. If no scheduler is provided, then the timeout scheduler is used.

Here is an example of the delay operator:

numbers = Observable.just(1)

print("{}".format(datetime.datetime.now()))
numbers.delay(200).subscribe(
on_next = lambda i: print("on_next {}: {}"
.format(i, datetime.datetime.now())),
on_error = lambda e: print("on_error: {}".format(e)),
on_completed = lambda: print("on_completed")
)
time.sleep(0.5)

In this example, the numbers operator emits just one item, and its emission is delayed by 200 milliseconds.

This example gives the following result:

2018-08-15 23:43:39.426395
on_next 1: 2018-08-15 23:43:39.628009
on_completed

Here, the item is received 200 milliseconds after the subscription.

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

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