[英]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 ?你好,代码有什么问题吗?
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.