This is a comparison of the different async/await strategies.
Python is the only language that doesn't execute the coroutines/tasks until you await for them.
# This program runs like the JS one (is the "fixed" version of the one with async/await syntax) | |
from asyncio import get_event_loop, ensure_future, coroutine | |
@coroutine | |
def third(): | |
print("third") | |
yield | |
return 3 | |
@coroutine | |
def second(): | |
print("second") | |
call = third() | |
next(call) | |
print("third:called") | |
yield | |
result = yield from call | |
print("third:finished", result) | |
return 2 | |
@coroutine | |
def first(): | |
print("first") | |
call = second() | |
next(call) | |
print("second:called") | |
result = yield from call | |
print("second:finished", result) | |
return 1 | |
async def main(): | |
await first() | |
loop = get_event_loop() | |
loop.run_until_complete(main()) | |
loop.close() | |
# This will output: | |
# first | |
# second | |
# third | |
# third:called | |
# second:called | |
# third:finished 3 | |
# second:finished 2 |
using System; | |
using System.Threading.Tasks; | |
class Program | |
{ | |
public static void Main() | |
{ | |
// Start the HandleFile method. | |
Task<int> task = first(); | |
} | |
static async Task<int> third() | |
{ | |
Console.WriteLine("third"); | |
return 3; | |
} | |
static async Task<int> second() | |
{ | |
Console.WriteLine("second"); | |
Task<int> task = third(); | |
Console.WriteLine("third:called"); | |
int result = await task; | |
Console.WriteLine("third:awaited " + result); | |
return 2; | |
} | |
static async Task<int> first() | |
{ | |
Console.WriteLine("first"); | |
Task<int> task = second(); | |
Console.WriteLine("second:called"); | |
int result = await task; | |
Console.WriteLine("second:awaited " + result); | |
return 1; | |
} | |
} | |
// Will output the following: | |
// first | |
// second | |
// third | |
// third:called | |
// third:awaited 3 | |
// second:called | |
// second:awaited 2 |
async function third() { | |
console.log("third"); | |
return 3; | |
} | |
async function second() { | |
console.log("second"); | |
let call = third(); | |
console.log("third:called"); | |
let result = await call; | |
console.log("third:awaited", result); | |
return 2; | |
} | |
async function first() { | |
console.log("first"); | |
let s = second(); | |
console.log("second:called"); | |
let result = await s; | |
console.log("second:awaited", result); | |
} | |
first().then(); | |
// Will output the following: | |
// first | |
// second | |
// third | |
// third:called | |
// second:called | |
// third:awaited 3 | |
// second:awaited 2 |
from asyncio import get_event_loop | |
async def third(): | |
print("third") | |
return 3 | |
async def second(): | |
print("second") | |
call = third() | |
print("third:called") | |
result = await call | |
print("third:awaited", result) | |
return 1 | |
async def first(): | |
print("first") | |
call = second() | |
print("second:called") | |
result = await call | |
print("second:awaited", result) | |
return 1 | |
async def main(): | |
await first() | |
loop = get_event_loop() | |
loop.run_until_complete(main()) | |
loop.close() | |
# Will output the following: | |
# first | |
# second:called | |
# second | |
# third:called | |
# third | |
# third:awaited 3 | |
# second:awaited 1 |