Basic asynchronous types

Microservices can be implemented in two different ways—synchronously and asynchronously. The approach refers to when the next task has to wait for the completion of the current task. To run tasks in parallel using code, we have to run a pool of threads and run tasks in the threads of the pool. The asynchronous approach is when you use non-blocking operations and a single thread performs multiple tasks. If an operation can't be completed, it returns a flag that means the task has not yet completed and we have to try to run it again later.

In the past, Rust developers used synchronous operations only, which meant that if we wanted to read data from a socket, we would have to block an executing thread. Modern operating systems have two approaches to avoid blocking—non-blocking input/output functions, and a scalable I/O event notification system, such as epoll.

Asynchronous activity refers to the ability to use the resources of the working for multiple concurrent activities. In contrast to synchronous handlers, asynchronous handlers use non-blocking operations. If resources are not available to finish the handler, it will be suspended until the next attempt to get access to resources. There is a well-established approach that involves a reactor and promises. A reactor allows a developer to run multiple activities in the same thread, while a promise represents a delayed result that will be available later. A reactor keeps a set of promises and continues to poll until it is completed and the result is returned.

Since the standard Rust library doesn't contain useful modules to write asynchronous applications and to work with reactors and promises, you need a third-party crate. An example of this type of crate is the futures crate, which we have used indirectly by using the hyper crate. It's now time to explore this crate in detail. In this section, we will discuss the different types of the futures crate that are available, how to use channels to pass messages between tasks, and how to use reactors, which are needed to run tasks.

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

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