Tokio का परिचय
async tasks को कैसे चलाना है।In this page:
#[tokio::main]
async fn main() {
let handle = tokio::spawn(async {
// task
});
handle.await.unwrap();
}
Adding Tokio as a Dependency
Tokio उपयोग करना इसे Cargo.toml में एक external dependency के रूप में जोड़ने से शुरू होता है, क्योंकि यह Rust standard library का हिस्सा नहीं है और इसे crates.io से fetch किया जाना चाहिए।
उदाहरण: Adding Tokio as a Dependency
[dependencies]
tokio = { version = "1", features = ["full"] }
⚠️ Run this in your own terminal or Node.js environment.
The #[tokio::main] Attribute
#[tokio::main] macro async fn main() के ऊपर रखा जाता है, इसे एक असली program entry point में बदलते हुए पर्दे के पीछे Tokio का async runtime setup करके।
यह snippet concept रूप से ऐसे code की shape दिखाता है; इसे असल में चलने के लिए tokio crate और network access चाहिए।
उदाहरण: The #[tokio::main] Attribute
// Conceptual shape only -- requires the tokio crate (not available offline):
//
// #[tokio::main]
// async fn main() {
// let result = say_hello().await;
// println!("{}", result);
// }
//
// async fn say_hello() -> String {
// String::from("Hello from an async task")
// }
fn main() {
println!("This shows the shape of #[tokio::main] code without running it");
println!("Real Tokio programs need the crate fetched from crates.io");
}
Login to try C/C++/Java/PHP code in the editor
What Tokio Provides
सिर्फ futures चलाने से आगे, Tokio async-aware networking, timers, channels, और task spawning प्रदान करता है, सभी एक cooperative scheduler के तहत efficiently एक साथ काम करने के लिए design किए गए।
उदाहरण: What Tokio Provides
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `features`, set to `["async TCP/UDP networking", "timers and sleep", "task spawning", "channels for message passing"]`
let features = ["async TCP/UDP networking", "timers and sleep", "task spawning", "channels for message passing"];
// Loop over `features.iter()`, binding each item to `f`
for f in features.iter() {
// Print "Tokio provides: {}" to the console, substituting in `f` (with a trailing newline)
println!("Tokio provides: {}", f);
}
}
Login to try C/C++/Java/PHP code in the editor
Why This Example Cannot Run Here
चूंकि इस course के code runner के पास crates.io से crates download करने के लिए network access नहीं, एक असली Tokio-based program को इस environment में compile या execute नहीं किया जा सकता -- यहां सिर्फ plain standard-library Rust चल सकता है।
उदाहरण: Why This Example Cannot Run Here
// Entry point: execution of the program starts here
fn main() {
// Print "Real async runtimes like Tokio require fetching an external crate" to the console (with a trailing newline)
println!("Real async runtimes like Tokio require fetching an external crate");
// Print "Without network access, only std-library-only Rust can compile and run here" to the console (with a trailing newline)
println!("Without network access, only std-library-only Rust can compile and run here");
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि Tokio Rust standard library का हिस्सा है -- यह एक external crate है जिसे Cargo के ज़रिए एक dependency के रूप में जोड़ा जाना चाहिए।
- यह भूल जाना कि
#[tokio::main]वह है जो एकasync fn main()को एक regularfn main()में बदलता है जो runtime setup और चलाता है। - बिना network access के networked या timer-based async examples चलाने की कोशिश करना, जो इस course के code runner जैसे offline sandbox में काम नहीं करेगा।
- Tokio एक widely used asynchronous runtime crate है,
Cargo.tomlमें एक external dependency के रूप में जोड़ा गया। #[tokio::main]attribute macro एकasync fn main()को Tokio का executor setup करके एक runnable program में बदलता है।- Tokio
Futuretrait के आस-पास बने I/O, timers, और task spawning के async-friendly versions प्रदान करता है। - चूंकि Tokio को crates.io से download किया जाना चाहिए, इसे network access चाहिए और यह पूरी तरह offline sandbox में नहीं चल सकता।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: