簡體   English   中英

Python 作為線程執行的異步和同步函數

[英]Python async and sync functions executed as threads

我想從純同步代碼中調用異步 function。 我想在后台執行異步 function 而不會卡住我的編。 我的想法是使用線程模塊。

from threading import Thread
import asyncio

async def func1():
    ...

def func2():
    ...

if __name__ == '__main__':
    Thread(target=func1).start()
    Thread(target=func2).start()

任何想法? 提前致謝!

由於 Python 3.7 有asyncio.run

代替

    Thread(target=func1).start()

經過

    Thread(target=asyncio.run, args=(func1(),)).start()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM