【问题标题】:How to use asyncio with threading in Python is Method threading.Timer如何在 Python 中将 asyncio 与线程一起使用是 Method threading.Timer
【发布时间】:2021-10-14 20:15:57
【问题描述】:
import asyncio, redis
from threading import Thread, Timer
db = redis.StrictRedis(host='localhost', port=6379, db=0, charset='UTF-8', decode_responses=True)
txts = ['txt1','txt2','txt3']

async def Send_GP():
    async for dialog in bot.iter_dialogs():
        if dialog.is_group:
            text = random.choice(txts)
            await bot.send_message(dialog.id, text)
            


async def send_chat():
    try:
        t = asyncio.get_event_loop().create_task(send_chat)
        await t()
        db.setex('timeleft:',int(db.get('time_chat:')),True)
        Timer(int(db.get('time_chat:')), t, []).start()
    except Exception as e:
        print(e)

你好,代码有什么问题?


是我的错误:

从未等待协程“send_chat” self.function(*self.args, **self.kwargs)

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: python multithreading bots python-asyncio telethon


【解决方案1】:

这个错误实际上总是意味着在 async 函数后面的某处缺少一个 await 关键字。检查调用send_chat 的所有位置。

另外,您似乎在t = asyncio.get_event_loop().create_task(send_chat) 行对send_chat 进行某种递归调用,这可能不是有意的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    相关资源
    最近更新 更多