Introduction to asynchronous programming

If you want to achieve high performance in computing, you will need to run tasks concurrently. Whether you are running complex computations that take days, such as machine learning training, or you are running a web server that needs to respond to thousands of requests per second, you will need to do more than one thing at the same time.

Thankfully, as we have already seen, our processors and operating systems are prepared for concurrency, and in fact, multithreading is a great way to achieve it. The main issue is that as we saw in the previous chapter, we should not be using more threads than logical CPUs in our computer.

We can, of course, but some threads will be waiting for others to execute, and the kernel will be orchestrating how much time each thread gets in the CPU. This will consume even more resources and make the overall process slower. It can sometimes be useful, though, to have more threads than the number of cores. Maybe some of them only wake up once every few seconds to do small tasks, or we know that most of them will block due to some I/O operation.

When a thread blocks, the execution stops. No further instruction will run in the CPU until it gets unblocked. This can happen when we read a file, for example. Until the reading ends, no further instruction will be executed. This of course, depends on how we read the file.

But in the latter case, instead of creating more threads we can do better—asynchronous programming. When programming asynchronously, we let the code continue being executed while we are still waiting for a certain result. That will avoid blocking the thread and let you use less threads for the same task, while still being concurrent. You can also use asynchronous programming for tasks not related to I/O, but if they are CPU bound (their bottleneck is the CPU), you won't get speed improvements, since the CPU will always be running at its best. To learn how asynchronous I/O works in Rust, let's first dive into how the CPU handles I/O.

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

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