python非同步執行緒,實現協程操作
阿新 • • 發佈:2022-05-11
- 單個方法簡單實現非同步 async
# 引入 asyncio 模組才能執行協程函式 import asyncio #使用 async 關鍵字將函式定義為協程函式 async def func(): print("Hello World~~~~") if __name__=='__main__': g = func() # 此時的 func() 是非同步協程函式, 函式執行得到的是一個協程物件 print("~~~~~~~~~~~~~~~") asyncio.run(g) # 協程模組需要 asyncio 的支援
- 多個非同步方法實現協程操作
# 執行非同步函式引入 asyncio