from aiogram import Bot, Dispatcher
from aiogram.fsm.storage.memory import MemoryStorage
import threading
import logging
import asyncio
import config as cf
from handler.start import start_router
from hand_callback.hand_callback import hand_router
from handler.admin import admin_router
from handler.state_handler import state_handler
from database import queue_silver
logging.basicConfig(level=logging.INFO)
bot = Bot(cf.API)
dp = Dispatcher(storage=MemoryStorage())
async def main():
dp.include_routers(start_router, hand_router, admin_router, state_handler)
thread = threading.Thread(target=queue_silver.checking)
thread.start()
await asyncio.sleep(6)
await dp.start_polling(bot)
if __name__ == '__main__':
print("Бот запущен")
try:
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
except Exception as e:
logging.error(f"An error occurred: {e}")
帮我修复错误
它给出的错误是:
C:\Users\user\AppData\Local\Programs\Python\Python38\lib\threading.py:870:
RuntimeWarning: coroutine 'checking' was never awaited
self._target(*self._args, **self._kwargs)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
RuntimeWarning: coroutine 'checking' was never awaited当使用但不带括号调用异步函数await(或以其他方式不等待其结果)时,会出现该形式的警告。出现这种情况的问题是因为您将异步函数作为目标传递给
threading.Thread,但它对异步函数一无所知,并尝试将其作为常规函数运行。queue_silver.checking您可以通过使用以下命令启动后台异步任务来替换通过线程的调用尝试来解决此问题asyncio.create_task:await,因为你不需要立即获取结果,但你需要任务在后台执行checking(如果该函数有参数,则带参数)