python - Python Bot报错原因及解决方法
问题描述
我在共享主机中运行一个 python 机器人,它运行良好,但每天几个小时后,它会崩溃 1 或 2 次。那么是什么导致了这个错误以及如何解决这个问题。
RuntimeError: can't start new thread
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7f80a690b6d8>
Task exception was never retrieved
future: <Task finished coro=<WebSocketCommonProtocol.run() done, defined at /home/demotry/.local/lib/python3.6/site-packages/websockets/protocol.py:428> exception=ConnectionResetError(104, 'Connection reset by peer')>
Traceback (most recent call last):
File "/home/demotry/.local/lib/python3.6/site-packages/websockets/protocol.py", line 434, in run
msg = yield from self.read_message()
File "/home/demotry/.local/lib/python3.6/site-packages/websockets/protocol.py", line 456, in read_message
frame = yield from self.read_data_frame(max_size=self.max_size)
File "/home/demotry/.local/lib/python3.6/site-packages/websockets/protocol.py", line 511, in read_data_frame
frame = yield from self.read_frame(max_size)
File "/home/demotry/.local/lib/python3.6/site-packages/websockets/protocol.py", line 546, in read_frame
self.reader.readexactly, is_masked, max_size=max_size)
File "/home/demotry/.local/lib/python3.6/site-packages/websockets/framing.py", line 86, in read_frame
data = yield from reader(2)
File "/home/demotry/.local/lib/python3.6/asyncio/streams.py", line 674, in readexactly
yield from self._wait_for_data('readexactly')
File "/home/demotry/.local/lib/python3.6/asyncio/streams.py", line 464, in _wait_for_data
yield from self._waiter
File "/home/demotry/.local/lib/python3.6/asyncio/selector_events.py", line 723, in _read_ready
data = self._sock.recv(self.max_size)
ConnectionResetError: [Errno 104] Connection reset by peer
Task was destroyed but it is pending!
task: <Task pending coro=<WebSocketCommonProtocol.run() running at /home/demotry/.local/lib/python3.6/site-packages/websockets/protocol.py:434> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f80a6970f48>()]>>
我添加了我的 bot.py 脚本代码,如下所示,其中一些命令只有一个 bot.py,没有其他文件。一切正常,但一天崩溃 2 或 3 次。
token = "This is my Token" # This is what the bot uses to log into Discord.
prefix = "?" # This will be used at the start of commands.
import discord
from discord.ext import commands
from discord.ext.commands import Bot
bot = commands.Bot(command_prefix=prefix)
bot.remove_command("help")
@bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print(discord.__version__)
print('------')
@bot.command(pass_context=True)
async def ping(ctx):
msg = 'Pong {0.author.mention}'.format(ctx.message)
await bot.say(msg)
@bot.command(pass_context=True)
async def hello(ctx):
msg = 'hello... {0.author.mention}'.format(ctx.message)
await bot.say(msg)
bot.run(token)
解决方案
推荐阅读
- java - 如何获取在 Java 8 上编译的 Eclipse 库
- java - log4j2 RollingFile 不使用 filePattern 的日志消息中的日期,而是使用当前日期
- javascript - Unlayer 电子邮件编辑器 - 如何禁用退出查看模式的功能
- certificate - 有没有办法检查证书是客户端证书还是服务器证书?
- visual-studio - 我在 CMD + S 上的样式更改而无需修改
- json - 如何使用 swift 在 Xcode 中传递 JSON 数组视图控制器
- python - 从 PiCam 保存的视频的快进问题
- c# - 从 ASP.NET Core Web API 应用程序访问 OAuth2 端点
- netsuite - 如何使用 SuiteQL 获取 NetSuite 公司的名称?
- amazon-ec2 - 安全组的 AWS IAM 策略不起作用