Getting started with Tokio. The ultimate starter guide to writing async Rust.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Tokyo is an asynchronous runtime for the rust programming language that provides the building blocks needed for writing efficient and scalable Network applications to get started with Tokyo we can create a new project using cargo and add the Tokyo crate to the cargo.tomel with Tokyo installed we can create an instance of the Tokyo runtime in our main function next we call our run function which returns a future we'll discuss Futures in more detail later on but you can think of them similar to promises in node or other languages we can then pass this future into the block on function of the runtime the future executes and calls the code inside of the Run function which will sleep for one second pretty simple this is a really basic example of using the async and a weight keywords but nothing much is happening here we could have quite easily done this synchronously let's add something else to spice it up a little now our run function is going to call two other functions first are sleeper function which is basically the same code we had before add our reader function which is loading a data file reading to the end and telling us how many bytes are in that file when we run this code we can see again it's running synchronously nothing special let's make a change and actually wrap these two functions in the join function of the Tokyo module in the output we can now see that both functions occurred at the same time AKA concurrently to further explain this point let's go ahead and do this again but calling the reader function 10 times pretty cool for reference here's how long this takes if we perform this synchronously almost double the amount of time due to the lack of concurrency so that's pretty great maybe we should just be using Tokyo for everything right well not exactly Tokyo is good for non-blocking Io which is when an application is waiting on operations such as reading from a file or network data however there are situations where the bottleneck of an application is CPU or performance bound Tokyo uses a single thread for its main event Loop therefore if any tasks are performing heavy CPU based operations this will actually slow down the other asynchronous tasks that are running to demonstrate this I've gone ahead and added in a Fibonacci calculation to each of the ridicules which is expensive on the CPU by adding in this calculation we've dramatically increased the runtime of our application fortunately there is a way to get the performance back we can use the task model of the Tokyo framework to sport a task which will run in a different thread and just like that we're performing again oh I forgot to mention a little quality of life Improvement we can actually use the Tokyo main macro instead of creating a new instance of the runtime this turns our main function into an async function that returns a future neat okay so now you're a master of asynchronous rust it's probably time to discuss Futures and how they differ from promises so it may not be very visible from the code that we've written but we've been interacting with the type known as Futures this whole time a future represents a computation that will be completed in the future they are the essential building blocks of asynchronous code when compared to other languages they are most like a promise however with one major difference they are lazy this means they don't execute as soon as you create them but instead only once they are explicitly pulled by the Tokyo runtime which is typically when the away keyword is used let's highlight this with an example let's cool our sleeper function twice using the await keyword each time here we get a log output for each execution of the function now if we remove the await keyword from the first lipical there's only a single execution that takes place the first execution never happens the compiler does actually give us a warning which is nice to see whilst this might seem off there's a good reason for this approach it improves performance of the runtime as it avoids having to constantly check whether a future is ready to execute well what if you want to execute the future but no wait around for its completion sort of like a fire and forget well we can do this using the Tokyo spawn method Futures are returned implicitly whenever we use the async keyword which basically acts as syntactical sugar it also allows you to use the await keyword which can only be called inside of an async function now that we've covered the basics of Tokyo and Futures it's worth a brief look at some of the other features that the package provides I'll be doing more in-depth videos on each of these in the future so consider subscribing if you don't want to miss them the Tokyo sync module provides functionality for communication and synchronization across concurrent tasks it provides both async counterparts to the synchronization types of the standard Library such as mutex read write locks semaphores and also provides some novel types used for cross-task communication known as channels these are similar to channels that you would find in other languages such as go but basically allow data to be sent from one asynchronous task to another the net module provides low-level Network Primitives for working with TCP and UTP sockets asynchronously some examples of the provided type are a TCP listener for accepting TCP connections as a server or a TCP socket for making connections to the server the Tokyo task module provides asynchronous green threads green threads are similar to operating system threads but are much more lightweight and are managed by the Tokyo runtime instead these are similar to goes go routines kotlin's co-routines or erlang or Alexa's processors the fs module provides asynchronous apis for working with the file system it includes functions for reading and writing files as well as for managing directories symbolic links and permissions the process module provides asynchronous Process Management it includes functions for spawning child processes as well as communicating with them through pipes using the process module one can spawn other processes and wait for them in an asynchronous non-blocking manner the signal module provides asynchronous handling of operating system signals such as interrupts allowing for graceful termination of an application the time module provides asynchronous apis for working with time it includes functions for delaying execution as well as scheduling periodic tasks the module also has support for setting timeouts on Futures using the timeout function which can be useful for cancellation of long-running execution and the Tokyo test macro provides the ability to test asynchronous code concurrently by using the macro with a test case we're able to use the async and a weight keyword with our test functions with all of this functionality provided by the Tokyo framework it's possible to create i o based applications that are both performant and scalable I hope this video helped to encourage you to play around with asynchronous programming in Rust and inspired you for your next project
Info
Channel: Dreams of Code
Views: 64,757
Rating: undefined out of 5
Keywords: async, async await, async/await, asynchronous, await, codding, coding, fast code, green threads, io, learn rust, netowork rust, performance, programming, rust, rust async, rust fs, rust lang, rust language, rust net, rust programming, rust programming tutorial, rust tut, rustlang, tokio, tokio rust, tutorial
Id: dOzrO40jgbU
Channel Id: undefined
Length: 7min 4sec (424 seconds)
Published: Wed Mar 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.