简体   繁体   English

如何在 Python 中将 asyncio 与线程一起使用是 Method threading.Timer

[英]How to use asyncio with threading in Python is Method threading.Timer

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)

Hello, what is the problem with the code ?你好,代码有什么问题吗?


is my error :是我的错误:

coroutine 'send_chat' was never awaited self.function(*self.args, **self.kwargs)协程 'send_chat' 从未等待过 self.function(*self.args, **self.kwargs)

This error practically always means that there is an await keyword missing somewhere behind an async function.这个错误实际上总是意味着在async函数后面的某个地方缺少await关键字。 Check everywhere where send_chat is called.检查调用send_chat所有地方。

Also, it looks like you are making some sort of recursive call of send_chat at line t = asyncio.get_event_loop().create_task(send_chat) , which is probably not intended.此外,看起来您正在t = asyncio.get_event_loop().create_task(send_chat)行对send_chat进行某种递归调用,这可能不是故意的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM