I have the following script written in both js and python. However, I cannot get the python script to work.
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function get_burgers() {
console.log("Just ordered...");
console.log("Making burgers");
await sleep(3000);
console.log("Burgers ready!");
console.log("Eating them...");
}
async function main() {
console.log("Walking into restaurant...");
console.log("Ordering...");
get_burgers();
console.log("Talking, talking, talking...");
}
main();
This outputs:
Walking into restaurant...
Ordering...
Just ordered...
Making burgers
Talking, talking, talking...
Burgers ready!
Eating them...
This is the same script in Python using asyncio
import asyncio
async def get_burgers():
print("Just ordered...")
print("Making burgers...")
await asyncio.sleep(5)
print("Burgers ready!")
print("Eating burgers")
async def main():
print("Walking into store with crush")
print("Ordering....")
await asyncio.gather(get_burgers())
print("Talking, talking, talking...")
asyncio.run(main())
This blocks and prints Talking, talking, talking... last. When I remove the await before asyncio.gather(get_burgers()) it crashes. How would I make the python script give the result as the node.js script?