Understanding Asynchronous Operations with Futures Crate

Rust is a modern language and has many approaches and crates that we can use to implement microservices. We can split these into two categories—synchronous frameworks and asynchronous frameworks. If you want to write synchronous microservices, you can implement a handler as a sequence of expressions and methods calls. But writing asynchronous code is hard in Rust, because it doesn't use a garbage collector and you have to take into account the lifetimes of all objects, including callbacks. This is not a simple task, because you can't stop the execution at any line of the code. Instead, you have to write code that won't block the execution for a long period of time. This challenge can be elegantly solved with the futures crate.

In this chapter, you will learn about how the  futures crate works. We will study two basic types—Future and Stream. We will also explore the Multi-Producer Single-Consumer (MPSC) module, which is an alternative to a similar module of the std crate, but supports asynchronous access to channels. At the end of the сhapter, we will create a microservice that uses Future and Stream traits to process incoming data and return a processed result to a client.

The futures crate contains asynchronous primitives only. We will also use the tokio crate, which provides asynchronous input and output capabilities to read and write image files for our microservice.

We will cover the following topics in this chapter:

  • Basic asynchronous types
  • Creating an image service

 

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

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