Async/Await के Basics
async और .await एक program को किसी धीमी चीज़ का इंतज़ार करते हुए एक task रोकने देते हैं, और इस बीच दूसरे काम के साथ आगे बढ़ने देते हैं।In this page:
async fn name() -> ReturnType {
let value = other().await;
value
}
Declaring an Async Function
एक async fn एक normal function जैसा दिखता है लेकिन एक Future return करता है जो इसका eventual result represent करता है, call होने पर तुरंत इसकी body चलाने के बजाय।
Async syntax को actually execute करने के लिए 2018+ Rust edition और एक runtime चाहिए, इसलिए यह snippet shape को एक comment के रूप में दिखाता है और underlying idea (एक value जो तुरंत तैयार नहीं है) को plain std code से demonstrate करता है।
उदाहरण: Declaring an Async Function
// Async syntax (needs edition 2018+ and a runtime), shown conceptually:
// async fn compute() -> i32 {
// 42
// }
fn main() {
println!("async fn compute() would return a Future, not an i32 directly");
println!("It must be awaited inside an async runtime to actually run");
}
Login to try C/C++/Java/PHP code in the editor
The Role of .await
किसी async context के अंदर, .await एक Future पर उपयोग होता है इसे completion तक drive करने के लिए, उस task को रोकते हुए (पूरे program को block किए बिना) जब तक value तैयार न हो।
चूंकि .await को edition 2018+ और एक runtime चाहिए, यह snippet async shape को एक comment के रूप में रखता है और इसके बजाय plain function calls से conceptual chaining दिखाता है।
उदाहरण: The Role of .await
// Conceptual async chain (needs edition 2018+ and a runtime):
// async fn get_value() -> i32 { 10 }
// async fn use_value() -> i32 {
// let v = get_value().await;
// v + 1
// }
fn get_value() -> i32 {
10
}
fn use_value() -> i32 {
let v = get_value();
v + 1
}
fn main() {
println!("Synchronous stand-in result: {}", use_value());
println!("An async version would use .await instead of a direct call");
}
Login to try C/C++/Java/PHP code in the editor
Why Async Needs a Runtime
Rust standard library Future trait और async/.await syntax define करती है, लेकिन futures को असल में चलाने के लिए एक executor शामिल नहीं करती -- वह काम Tokio या async-std जैसे एक runtime crate से belong करता है।
उदाहरण: Why Async Needs a Runtime
// Entry point: execution of the program starts here
fn main() {
// Print "The standard library defines Future and async/.await syntax" to the console (with a trailing newline)
println!("The standard library defines Future and async/.await syntax");
// Print "But executing futures requires an external runtime crate" to the console (with a trailing newline)
println!("But executing futures requires an external runtime crate");
// Print "Common choices: tokio, async-std" to the console (with a trailing newline)
println!("Common choices: tokio, async-std");
}
Login to try C/C++/Java/PHP code in the editor
Async Is for Concurrency, Not Parallelism
Async/await तब चमकता है जब कोई program एक साथ कई धीमे I/O operations (जैसे network requests) का इंतज़ार कर रहा हो, एक single thread को उन्हें efficiently juggle करने देते हुए, raw CPU-bound computation तेज़ करने के बजाय।
उदाहरण: Async Is for Concurrency, Not Parallelism
// Entry point: execution of the program starts here
fn main() {
// Print "Async excels at juggling many waiting I/O tasks concurrently" to the console (with a trailing newline)
println!("Async excels at juggling many waiting I/O tasks concurrently");
// Print "For CPU-bound parallel work, OS threads are usually the better tool" to the console (with a trailing newline)
println!("For CPU-bound parallel work, OS threads are usually the better tool");
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि कोई
async fncall होते ही तुरंत चलता है -- यह असल में एक lazyFuturereturn करता है जो तब तक कुछ नहीं करता जब तक इसे await या poll न किया जाए। - Futures को असल में completion तक drive करने वाले एक async runtime (जैसे Tokio या async-std) के बिना async code चलाने की कोशिश करना।
- यह भूल जाना कि
.awaitसिर्फ किसीasync fnया async block के अंदर उपयोग किया जा सकता है, ordinary synchronous code में नहीं।
- एक
async fnतुरंत एकFuturereturn करता है, उस काम को represent करते हुए जो अभी शुरू नहीं हुआ। .awaitएकFutureको completion की ओर drive करता है, इसके इंतज़ार के दौरान control runtime को वापस देते हुए।- Async code को futures को असल में execute करने के लिए एक runtime (जैसे Tokio) चाहिए -- अकेली standard library उन्हें नहीं चलाती।
- Async/await कई I/O-bound tasks पर efficiently इंतज़ार करने के लिए design किया गया है, CPU-bound parallelism के लिए नहीं।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: