如何在Python中使用asyncio和threading是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)

你好,代码有什么问题?


是我的错误:

协程'send_chat'未等待self.function(*自我。args, * * self.kwargs)

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

而且,看起来您正在t = asyncio.get_event_loop().create_task(send_chat)行对send_chat进行某种递归调用,这可能不是有意的。