提问人:spirledaxis 提问时间:10/30/2023 更新时间:10/30/2023 访问量:12
使用 asyncio 的 RunTimeError
RunTimeError with asyncio
问:
我正在尝试运行一个单独的循环,同时让我的 discord 机器人执行其他操作,例如斜杠命令。我正在使用 asyncio、await 和 threading 来做到这一点。
bot = Client(intents=Intents.DEFAULT)
async def timeout_police(timeout2, interval, client):
print("Delivered client", client)
print("Called func")
while True:
await asyncio.sleep(interval) # should be 60
#define elapsed_time
if elapsed_time > timeout2: # should be 7200 for 2 hours
print("A run has timed out!")
await client.get_user(MYID).send("Run has timed out") #Problem Here
else:
print(f"{elapsed_time} is not at the timeout value {timeout2}")
async def timeout_check(client):
print("Called timeout Check")
while True:
await timeout_police(15, 5, client)
def thread_function(client): #use threading so this can run with the bot
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(timeout_check(client))
@listen() #discord bot starts here
async def on_ready():
timeout_thread = threading.Thread(target=thread_function, args=(bot,))
timeout_thread.start()
print("Ready")
该错误是由我假设使用 await 有问题引起的,尽管我不确定问题到底是什么。我得到await client.get_user(MYID).send("Run has timed out")
RuntimeError: Timeout context manager should be used inside a task
答: 暂无答案
评论