英文:
How do I run a discord py bot asynchronously with another async function?
问题
I tried many ways, but I was unable to run the bot correctly in a thread.
Tried this simple approach using threading
import threading
sync_thread = threading.Thread(target=self.run, args=(DISCORD_BOT_TOKEN,))
async_thread = threading.Thread(target=asyncio.run, args=(self.start_alerts() ,))
sync_thread.start()
async_thread.start()
# Wait for both threads to complete
sync_thread.join()
async_thread.join()
但是在以下这行代码上出现了以下错误。我不仅在fetch_user
这里出现了这个错误,而且在像发送消息到频道这样的任何机器人操作中都出现了这个错误。
admin = await self.fetch_user(DISCORD_ADMIN_ID)
File "/venv/lib/python3.10/site-packages/discord/client.py", line 2525, in fetch_user
data = await self.http.get_user(user_id)
File "/venv/lib/python3.10/site-packages/discord/http.py", line 624, in request
async with self.__session.request(method, url, **kwargs) as response:
File "/venv/lib/python3.10/site-packages/aiohttp/client.py", line 1141, in __aenter__
self._resp = await self._coro
File "/venv/lib/python3.10/site-packages/aiohttp/client.py", line 467, in _request
with timer:
File "/venv/lib/python3.10/site-packages/aiohttp/helpers.py", line 701, in __enter__
raise RuntimeError(
RuntimeError: Timeout context manager should be used inside a task
英文:
I want the bot to run in one thread, and I want an async function to run in another. This async function will basically check if it is 2 pm, and send messages to all the servers it is in. If it is not 2 pm, the thread will sleep.
I tried many ways, but I was unable to run the bot correctly in a thread.
Tried this simple approach using threading
import threading
sync_thread = threading.Thread(target=self.run, args=(DISCORD_BOT_TOKEN,))
async_thread = threading.Thread(target=asyncio.run, args=(self.start_alerts() ,))
sync_thread.start()
async_thread.start()
# Wait for both threads to complete
sync_thread.join()
async_thread.join()
But it gives the following error on this line. I'm getting this error not just on fetch_user
but on any bot operation like sending message to a channel.
admin = await self.fetch_user(DISCORD_ADMIN_ID)
File "/venv/lib/python3.10/site-packages/discord/client.py", line 2525, in fetch_user
data = await self.http.get_user(user_id)
File "/venv/lib/python3.10/site-packages/discord/http.py", line 624, in request
async with self.__session.request(method, url, **kwargs) as response:
File "/venv/lib/python3.10/site-packages/aiohttp/client.py", line 1141, in __aenter__
self._resp = await self._coro
File "/venv/lib/python3.10/site-packages/aiohttp/client.py", line 467, in _request
with timer:
File "/venv/lib/python3.10/site-packages/aiohttp/helpers.py", line 701, in __enter__
raise RuntimeError(
RuntimeError: Timeout context manager should be used inside a task
答案1
得分: 0
找到了解决这个问题的答案。将其添加在这里,而不是删除这个问题。
https://stackoverflow.com/a/73903794/11721258
英文:
Found an answer that solves this issue. Adding it here instead of deleting the question.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论