Chapter 5

Why is using a blocking function a problem in an asynchronous application?

When a blocking function is called, the event loop is also blocked. This means that during the whole execution of the blocking function, no other action can occur in the application. Blocking calls are often I/O-based operations that can be very long (up to several seconds sometimes). If the application is stalled for such a long period, at best the user experience is degraded, and at worst some important messages will have been lost.

Why is using a CPU-intensive task a problem in an asynchronous application?

Using a CPU-bound task leads to the same issue as using a blocking call: the event loop is blocked for a long time, and it is just a matter of time before this breaks the functioning of the application.

What is the aim of a scheduler?

A scheduler is one of the ReactiveX components that allows us to integrate with asynchronous frameworks. The default behavior of ReactiveX is to be purely synchronous. This can seem counter-intuitive, but it is this property that allows us to integrate ReactiveX code easily with asynchronous frameworks such as AsyncIO. A scheduler only has to change the current execution context, with no other changes to the rest of the code. A scheduler changes the default synchronous behavior of subscriptions and makes them asynchronous, either running on an event loop or running on a worker thread. Several schedulers are available in RxPY to use different multithreading concurrency patterns, such as thread pooling.

What are the two possible ways to select the scheduler of a source observable?

The selection of a scheduler associated to a source observable can be done either at creation time or in the operators chain. Most factory operators accept a scheduler parameter that allows us to set the scheduler. In cases where the code creating the observable does not know which scheduler to use, the subscribe_on operator can be used once anywhere in the chain.

How is it possible to change the execution context inside a chain of operators?

Inside a chain of operators, it is possible to change the execution context of the operators by using the observe_on operator. The observe_on operator changes the execution context for all the operators that are after it in the chain.

Why is it very important to use AsyncIOScheduler in an AsyncIO reactive application?

If an observable is created without a scheduler, then all its associated chain will be processed during the call to subscribe. In this case, the subscription has completed before the event loop has started, which is probably not the intended behavior.

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

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