Rust adventures - Async [ Part 1 ]

栏目: IT技术 · 发布时间: 6年前

内容简介:A while ago I realized that I was a visual learner which can be frustrating at times since some concepts might take me a bit longer to fully understand until I create the proper mental image(s) for it (or somebody else does it for me).When I started wrappi

A while ago I realized that I was a visual learner which can be frustrating at times since some concepts might take me a bit longer to fully understand until I create the proper mental image(s) for it (or somebody else does it for me).

When I started wrapping my head around Async programming in Rust I felt like I was missing some of those images. What follows is my attempt visualize the concepts around async programing.

Traditional threaded applications

When creating multi-threading applications, if you wanted to execute multiple tasks at the same time you’ll need the same number of threads as tasks. So you’ll have a 1-1 mapping between tasks and threads. Let’s imagine for example, we wanted to execute 3 tasks:

use std::thread;

pub fn main() {
    thread::spawn(move || make_coffee_task());
    thread::spawn(move || waste_time_task());
    let handle = thread::spawn(move || do_laundry_task());
    // wait for other threads to complete
    handle.join().unwrap()
}

fn make_coffee_task() {
    // Mocha time!
    println!("Coffee done");
}

fn do_laundry_task() {
    // Mostly gym clothing so it should be quick!
    println!("Laundry done");
}

fn waste_time_task() {
    // Checks twitter...
    println!("Done wasting time!");
}

It might looks like this at the Operating System (OS) level:

Rust adventures - Async [ Part 1 ]

During the execution of these threads, any of them can become blocked ( unable to continue to do work ) or they could just voluntarily yield ( programmer knew the thread will have nothing to do for some time under certain condition ), at that point the OS will have to save the state of the running thread (so that it can resume running at a later point) and select another to start or continue running. This is called thread-context switch.

Rust adventures - Async [ Part 1 ]

For a variety of reasons, this context switching has some amount of overhead that penalizes performance. Additionally, the approach doesn’t scale well since the more tasks we want to run at the same time the more threads we’ll need (eventually running out of resources).

Async

With Asynchronous code instead of using one thread per tasks, we can run multiple tasks concurrently on the same OS thread.

Because multiple tasks are running on the same OS thread, the issue of thread-context switching is greatly improved (less threads to switch around). Furthermore, since we’re reusing the same OS threads for multiple tasks, we end-up using significantly less resources.

For both of those reasons our Async counterpart has the potential of being much faster:

use futures; // Using futures = "0.3.1"
use std::thread;

async fn async_main() {
    let coffee_future = make_coffee_task();
    let laundry_future = do_laundry_task();
    let waste_future = waste_time_task();

    // Running the futures concurrently within the same thread
    futures::join!(coffee_future, laundry_future, waste_future);
}

fn main() {
    futures::executor::block_on(async_main());
}

async fn make_coffee_task() {
    // Mocha time!
    println!("Using thread Id: {:?} ", thread::current().id());
    println!("Coffee done! \n");
}

async fn do_laundry_task() {
    // Mostly gym clothing so it should be quick!
    println!("Using thread Id: {:?} ", thread::current().id());
    println!("Laundry done! \n");
}

async fn waste_time_task() {
    // Checks twitter...
    println!("Using thread Id: {:?} ", thread::current().id());
    println!("Done wasting time! \n");
}

This code is meant to act cooperatively in a single thread.

Rust adventures - Async [ Part 1 ]

In the next part we’ll be exploring the Future trait and the surrounding Async programming ecosystem further.

Footnotes:


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

架构真经

架构真经

马丁L. 阿伯特(Martin L. Abbott)、迈克尔T.费舍尔(Michael T. Fisher) / 机械工业出版社 / 2017-4 / 79

前言 感谢你对本书第2版感兴趣!作为一本入门、进修和轻量级的参考手册,本书旨在帮助工程师、架构师和管理者研发及维护可扩展的互联网产品。本书给出了一系列规则,每个规则围绕着不同的主题展开讨论。大部分的规则聚焦在技术上,少数规则涉及一些关键的思维或流程问题,每个规则对构建可扩展的产品都是至关重要的。这些规则在深度和焦点上都有所不同。有些规则是高级的,例如定义一个可以应用于几乎任何可扩展性问题的模......一起来看看 《架构真经》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

随机密码生成器
随机密码生成器

多种字符组合密码

html转js在线工具
html转js在线工具

html转js在线工具