簡體   English   中英

無法使用異步 function。 電視節目 python 異步

[英]can't get to work an async function. telethon python asyncio

我試圖理解 asyncio 並將其集成到我的程序中。

我試過這種方法:

def my_test():
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop.run_until_complete(my_function(group_id))

#//// code
    
async def my_function(group_id):
    group_id = (int(group_id))
    user = await client.get_entity(PeerUser(group_id)) # a telethon method the get an entity 
    print (user)

我嘗試了不同的方法..但代碼在 user = 處停止,沒有任何錯誤,所以我沒有任何可搜索的內容

是的,因為我認為這是一件愚蠢的事情。 找不到與它相關的任何東西,所以我想這可以幫助某人:

當您調用 client.start() 時,您正在創建一個循環,並且使用 client.run_until_disconnected() 您基本上告訴 asyncio 在斷開客戶端之前不要停止代碼。 那么解決方案是什么? (閱讀和理解文檔 lmao)

    if to_search == "id" or to_search == "ID" or to_search == "Id":
        clr(); group_id = (input(f"{info}{r}Insert group id:{rs}\n>>> "))

-------> client.add_event_handler(func_name)<---- the async one

然后我注意到 func 執行了 6 次(不明白為什么)所以確保像這樣啟動異步 function:

        async def search_by_id(event):
           client.remove_event_handler(func_name)

暫無
暫無

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

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