简体   繁体   中英

Python async vs Node.js async: how to execute an async function in Python and then continue until async is done?

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?

The Python async model is a bit different – you'd need to do

import asyncio


async def get_burgers():
    print("Just ordered...")
    print("Making burgers...")
    await asyncio.sleep(5)
    print("Burgers ready!")
    print("Eating burgers")


async def chat_while_waiting_for_burgers():
    print("Talking, talking, talking...")


async def main():
    print("Walking into store with crush")
    print("Ordering....")
    await asyncio.gather(get_burgers(), chat_while_waiting_for_burgers())


asyncio.run(main())

so the output is

Walking into store with crush
Ordering....
Just ordered...
Making burgers...
Talking, talking, talking...
Burgers ready!
Eating burgers

may be also you can use _thread module

import _thread
import time

def get_burgers(x):
    print("Just ordered...")
    print("Making burgers...")
    time.sleep(5)
    print("Burgers ready!")
    print("Eating burgers")
    print("Eating them")


def main(x):
    print("Walking into store with crush")
    print("Ordering....")
    _thread.start_new_thread( get_burgers, ("Thread-1", ) )
    time.sleep(5)
    print("Talking, talking, talking...")


try:
   _thread.start_new_thread( main, ("Thread-1", ) )
except:
   print ("Error: unable to start thread")

while 1:
   pass

output:

Walking into store with crush
Ordering....
Just ordered...
Making burgers...
Talking, talking, talking...
Burgers ready!
Eating burgers
Eating them

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM