The timeout operator

The timeout operator raises an error if no item is received on the source observable after some time has elapsed. The following figure shows the marble diagram of this operator:

Figure 9.22: The timeout operator

Its prototype is the following:

Observable.timeout(self, duetime, other=None, scheduler=None)

The duetime parameter is the delay in milliseconds after an error is raised. The other parameter is an observable that must be emitted in the case of timeout. If the other observable is not provided, then a Timeout error is raised. The scheduler parameter allows us to provide a different scheduler from the default one. If this is not provided, then the timeout scheduler is used.

Here is an example of the timeout operator:

numbers = Subject()

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

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

The timeout operator is configured to 300 milliseconds, and a delay of 500 milliseconds is forced between the emission of items 2 and 3.

This example gives the following result:

on_next 1
on_next 2
on_error: Timeout

An error is effectively raised before item 3 is emitted.

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

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