异步的很简单的代码

from datetime import datetime
import asyncio
import time
def print__time(word):
    print(word,datetime.now().strftime("%Y:%m:%d:%H:%M:%S"))

def wait():
    time.sleep(5)
    print("等我5秒")
print__time("开始")
wait()
print__time("结束")
#这个就是直接停顿5秒
async def wait1():
    asyncio.sleep(5)
    print("等我5秒")
async def print__time1(word):
    print(word,datetime.now().strftime("%Y:%m:%d:%H:%M:%S"))
async def main():
    await print__time1("开始")
    await wait1()
    await print__time1("结束")
loop=asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
"""开始 2022:08:28:16:04:34
等我5秒
结束 2022:08:28:16:04:39
开始 2022:08:28:16:04:39
等我5秒
结束 2022:08:28:16:04:39"""

你可能感兴趣的:(python,开发语言)