2024-07-22 22:17:02 +08:00
|
|
|
# Async Design
|
|
|
|
|
|
|
|
|
|
## Async functions in different languages
|
|
|
|
|
|
|
|
|
|
### JavaScript
|
|
|
|
|
|
|
|
|
|
- [Async/Await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)
|
|
|
|
|
|
|
|
|
|
Prototype:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
async function name(param0) {
|
|
|
|
|
statements;
|
|
|
|
|
}
|
|
|
|
|
async function name(param0, param1) {
|
|
|
|
|
statements;
|
|
|
|
|
}
|
|
|
|
|
async function name(param0, param1, /* …, */ paramN) {
|
|
|
|
|
statements;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
```typescript
|
2024-07-22 22:35:02 +08:00
|
|
|
async function resolveAfter1Second(): Promise<string> {
|
2024-07-22 22:17:02 +08:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
setTimeout(() => {
|
2024-07-22 22:35:02 +08:00
|
|
|
resolve("Resolved after 1 second");
|
|
|
|
|
}, 1000);
|
2024-07-22 22:17:02 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
async function asyncCall(): Promise<string> {
|
|
|
|
|
const result = await resolveAfter1Second();
|
|
|
|
|
return `AsyncCall: ${result}`;
|
2024-07-22 22:17:02 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
async function asyncCall2(): Promise<string> {
|
|
|
|
|
const result = await resolveAfter1Second();
|
|
|
|
|
return `AsyncCall2: ${result}`;
|
2024-07-22 22:17:02 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
function asyncCall3(): void {
|
|
|
|
|
resolveAfter1Second().then((result) => {
|
|
|
|
|
console.log(`AsyncCall3: ${result}`);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
|
console.log("Starting AsyncCall");
|
|
|
|
|
const result1 = await asyncCall();
|
|
|
|
|
console.log(result1);
|
|
|
|
|
|
|
|
|
|
console.log("Starting AsyncCall2");
|
|
|
|
|
const result2 = await asyncCall2();
|
|
|
|
|
console.log(result2);
|
|
|
|
|
|
|
|
|
|
console.log("Starting AsyncCall3");
|
|
|
|
|
asyncCall3();
|
|
|
|
|
|
|
|
|
|
// Wait for AsyncCall3 to complete
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
|
|
|
|
|
|
|
|
console.log("Main function completed");
|
2024-07-22 22:17:02 +08:00
|
|
|
}
|
2024-07-22 22:35:02 +08:00
|
|
|
|
|
|
|
|
main().catch(console.error);
|
2024-07-22 22:17:02 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Python
|
|
|
|
|
|
|
|
|
|
- [async def](https://docs.python.org/3/library/asyncio-task.html#coroutines)
|
|
|
|
|
|
|
|
|
|
Prototype:
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
async def name(param0):
|
|
|
|
|
statements
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
import asyncio
|
|
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
async def resolve_after_1_second() -> str:
|
2024-07-22 22:17:02 +08:00
|
|
|
await asyncio.sleep(1)
|
2024-07-22 22:35:02 +08:00
|
|
|
return "Resolved after 1 second"
|
2024-07-22 22:17:02 +08:00
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
async def async_call() -> str:
|
|
|
|
|
result = await resolve_after_1_second()
|
|
|
|
|
return f"AsyncCall: {result}"
|
2024-07-22 22:17:02 +08:00
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
async def async_call2() -> str:
|
|
|
|
|
result = await resolve_after_1_second()
|
|
|
|
|
return f"AsyncCall2: {result}"
|
2024-07-22 22:17:02 +08:00
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
def async_call3() -> None:
|
|
|
|
|
asyncio.create_task(print_after_1_second())
|
|
|
|
|
|
|
|
|
|
async def print_after_1_second() -> None:
|
|
|
|
|
result = await resolve_after_1_second()
|
|
|
|
|
print(f"AsyncCall3: {result}")
|
2024-07-22 22:17:02 +08:00
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
|
print("Starting AsyncCall")
|
2024-07-22 22:35:02 +08:00
|
|
|
result1 = await async_call()
|
|
|
|
|
print(result1)
|
2024-07-22 22:17:02 +08:00
|
|
|
|
|
|
|
|
print("Starting AsyncCall2")
|
2024-07-22 22:35:02 +08:00
|
|
|
result2 = await async_call2()
|
|
|
|
|
print(result2)
|
2024-07-22 22:17:02 +08:00
|
|
|
|
|
|
|
|
print("Starting AsyncCall3")
|
|
|
|
|
async_call3()
|
|
|
|
|
|
|
|
|
|
# Wait for AsyncCall3 to complete
|
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
|
|
|
|
|
|
print("Main function completed")
|
|
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
# Run the main coroutine
|
|
|
|
|
asyncio.run(main())
|
2024-07-22 22:17:02 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Rust
|
|
|
|
|
|
|
|
|
|
- [async fn](https://doc.rust-lang.org/std/keyword.async.html)
|
|
|
|
|
|
|
|
|
|
Prototype:
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
async fn name(param0: Type) -> ReturnType {
|
|
|
|
|
statements
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
```rust
|
2024-07-22 22:35:02 +08:00
|
|
|
use tokio::time::{sleep, Duration};
|
2024-07-22 22:17:02 +08:00
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
async fn resolve_after_1_second() -> String {
|
2024-07-22 22:17:02 +08:00
|
|
|
sleep(Duration::from_secs(1)).await;
|
2024-07-22 22:35:02 +08:00
|
|
|
"Resolved after 1 second".to_string()
|
2024-07-22 22:17:02 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
async fn async_call() -> String {
|
|
|
|
|
let result = resolve_after_1_second().await;
|
|
|
|
|
format!("AsyncCall: {}", result)
|
2024-07-22 22:17:02 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
async fn async_call2() -> String {
|
|
|
|
|
let result = resolve_after_1_second().await;
|
|
|
|
|
format!("AsyncCall2: {}", result)
|
2024-07-22 22:17:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn async_call3() {
|
|
|
|
|
tokio::spawn(async {
|
2024-07-22 22:35:02 +08:00
|
|
|
let result = resolve_after_1_second().await;
|
|
|
|
|
println!("AsyncCall3: {}", result);
|
2024-07-22 22:17:02 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
|
async fn main() {
|
2024-07-22 22:35:02 +08:00
|
|
|
println!("Starting AsyncCall");
|
|
|
|
|
let result1 = async_call().await;
|
|
|
|
|
println!("{}", result1);
|
2024-07-22 22:17:02 +08:00
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
println!("Starting AsyncCall2");
|
|
|
|
|
let result2 = async_call2().await;
|
|
|
|
|
println!("{}", result2);
|
2024-07-22 22:17:02 +08:00
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
println!("Starting AsyncCall3");
|
2024-07-22 22:17:02 +08:00
|
|
|
async_call3();
|
|
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
// Wait for AsyncCall3 to complete
|
|
|
|
|
sleep(Duration::from_secs(1)).await;
|
|
|
|
|
|
|
|
|
|
println!("Main function completed");
|
2024-07-22 22:17:02 +08:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### C#
|
|
|
|
|
|
|
|
|
|
- [async](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/)
|
|
|
|
|
|
|
|
|
|
Prototype:
|
|
|
|
|
|
|
|
|
|
```csharp
|
|
|
|
|
async Task<ReturnType> NameAsync(Type param0)
|
|
|
|
|
{
|
|
|
|
|
statements;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
```csharp
|
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
class Program
|
2024-07-22 22:17:02 +08:00
|
|
|
{
|
2024-07-22 22:35:02 +08:00
|
|
|
static async Task<string> ResolveAfter1Second()
|
2024-07-22 22:17:02 +08:00
|
|
|
{
|
|
|
|
|
await Task.Delay(1000);
|
2024-07-22 22:35:02 +08:00
|
|
|
return "Resolved after 1 second";
|
2024-07-22 22:17:02 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
static async Task<string> AsyncCall()
|
2024-07-22 22:17:02 +08:00
|
|
|
{
|
2024-07-22 22:35:02 +08:00
|
|
|
string result = await ResolveAfter1Second();
|
|
|
|
|
return $"AsyncCall: {result}";
|
2024-07-22 22:17:02 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
static async Task<string> AsyncCall2()
|
2024-07-22 22:17:02 +08:00
|
|
|
{
|
2024-07-22 22:35:02 +08:00
|
|
|
string result = await ResolveAfter1Second();
|
|
|
|
|
return $"AsyncCall2: {result}";
|
2024-07-22 22:17:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void AsyncCall3()
|
|
|
|
|
{
|
2024-07-22 22:35:02 +08:00
|
|
|
_ = Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
string result = await ResolveAfter1Second();
|
|
|
|
|
Console.WriteLine($"AsyncCall3: {result}");
|
|
|
|
|
});
|
2024-07-22 22:17:02 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
static async Task Main()
|
2024-07-22 22:17:02 +08:00
|
|
|
{
|
|
|
|
|
Console.WriteLine("Starting AsyncCall");
|
2024-07-22 22:35:02 +08:00
|
|
|
string result1 = await AsyncCall();
|
|
|
|
|
Console.WriteLine(result1);
|
2024-07-22 22:17:02 +08:00
|
|
|
|
|
|
|
|
Console.WriteLine("Starting AsyncCall2");
|
2024-07-22 22:35:02 +08:00
|
|
|
string result2 = await AsyncCall2();
|
|
|
|
|
Console.WriteLine(result2);
|
2024-07-22 22:17:02 +08:00
|
|
|
|
|
|
|
|
Console.WriteLine("Starting AsyncCall3");
|
|
|
|
|
AsyncCall3();
|
|
|
|
|
|
|
|
|
|
// Wait for AsyncCall3 to complete
|
|
|
|
|
await Task.Delay(1000);
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Main method completed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### C++ 20 Coroutines
|
|
|
|
|
|
|
|
|
|
- [co_await](https://en.cppreference.com/w/cpp/language/coroutines)
|
|
|
|
|
|
|
|
|
|
Prototype:
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
TaskReturnType NameAsync(Type param0)
|
|
|
|
|
{
|
|
|
|
|
co_return co_await expression;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
```cpp
|
2024-07-22 22:35:02 +08:00
|
|
|
#include <cppcoro/task.hpp>
|
|
|
|
|
#include <cppcoro/sync_wait.hpp>
|
|
|
|
|
#include <cppcoro/when_all.hpp>
|
2024-07-22 22:17:02 +08:00
|
|
|
#include <chrono>
|
2024-07-22 22:35:02 +08:00
|
|
|
#include <iostream>
|
2024-07-22 22:17:02 +08:00
|
|
|
#include <thread>
|
|
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
cppcoro::task<std::string> resolveAfter1Second() {
|
|
|
|
|
co_await std::chrono::seconds(1);
|
|
|
|
|
co_return "Resolved after 1 second";
|
2024-07-22 22:17:02 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
cppcoro::task<std::string> asyncCall() {
|
|
|
|
|
auto result = co_await resolveAfter1Second();
|
|
|
|
|
co_return "AsyncCall: " + result;
|
2024-07-22 22:17:02 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
cppcoro::task<std::string> asyncCall2() {
|
|
|
|
|
auto result = co_await resolveAfter1Second();
|
|
|
|
|
co_return "AsyncCall2: " + result;
|
2024-07-22 22:17:02 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
cppcoro::task<void> asyncCall3() {
|
|
|
|
|
auto result = co_await resolveAfter1Second();
|
|
|
|
|
std::cout << "AsyncCall3: " << result << std::endl;
|
2024-07-22 22:17:02 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-22 22:35:02 +08:00
|
|
|
cppcoro::task<void> main() {
|
2024-07-22 22:17:02 +08:00
|
|
|
std::cout << "Starting AsyncCall" << std::endl;
|
2024-07-22 22:35:02 +08:00
|
|
|
auto result1 = co_await asyncCall();
|
|
|
|
|
std::cout << result1 << std::endl;
|
2024-07-22 22:17:02 +08:00
|
|
|
|
|
|
|
|
std::cout << "Starting AsyncCall2" << std::endl;
|
2024-07-22 22:35:02 +08:00
|
|
|
auto result2 = co_await asyncCall2();
|
|
|
|
|
std::cout << result2 << std::endl;
|
2024-07-22 22:17:02 +08:00
|
|
|
|
|
|
|
|
std::cout << "Starting AsyncCall3" << std::endl;
|
2024-07-22 22:35:02 +08:00
|
|
|
auto asyncCall3Task = asyncCall3();
|
2024-07-22 22:17:02 +08:00
|
|
|
|
|
|
|
|
// Wait for AsyncCall3 to complete
|
2024-07-22 22:35:02 +08:00
|
|
|
co_await asyncCall3Task;
|
2024-07-22 22:17:02 +08:00
|
|
|
|
|
|
|
|
std::cout << "Main function completed" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
2024-07-22 22:35:02 +08:00
|
|
|
try {
|
|
|
|
|
cppcoro::sync_wait(::main());
|
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
|
std::cerr << "Error: " << e.what() << std::endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2024-07-22 22:17:02 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
```
|