我正在使用线程模块同时托管网络服务器和 Discord 机器人。一切都在Windows上运行良好,但是一旦我将其加载到Linux服务器上,就会出现以下错误:
Starting Bot
Exception in thread Bot:
Traceback (most recent call last):
File "/usr/lib/python3.8/asyncio/unix_events.py", line 95, in add_signal_handler
signal.set_wakeup_fd(self._csock.fileno())
ValueError: set_wakeup_fd only works in main thread
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/home/webadmin/discordbot/bot/moduls/m_threadingmaker.py", line 15, in run
self.client.run(self.args[0])
File "/home/webadmin/discordbot/bot/venv/lib/python3.8/site-packages/discord/client.py", line 614, in run
loop.add_signal_handler(signal.SIGINT, lambda: loop.stop())
File "/usr/lib/python3.8/asyncio/unix_events.py", line 97, in add_signal_handler
raise RuntimeError(str(exc))
RuntimeError: set_wakeup_fd only works in main thread
我已经从python 3.7升级到python 3.8,但我仍然有同样的错误。
这是我的代码: main.py(网络服务器工作(
dcbot = m_threadingmaker.myThread("Bot", client, secrets.token)
webserver = m_threadingmaker.myThread("Flask", app, 'localhost', '7010')
#webserver.start()
dcbot.start()
M_threadingmaker.py
from threading import Thread
class myThread (Thread):
def __init__(self, name, client, *args):
Thread.__init__(self)
self.name = name
self.client = client
self.args = args
def run(self):
print("Starting " + self.name)
if self.name == "Flask":
self.client.run(host=self.args[0], port=self.args[1])
else:
self.client.run(self.args[0])
print("Exiting " + self.name)
webserver.start()
dcbot.run()
好的,现在它启动网络服务器和机器人。但是当我尝试在机器人启动后做某事时,没有任何反应。但是,我对为什么会这样感兴趣。如果有人知道一个好的和广泛的贡献,比如关于线程的书,请发送它
我建议您在异步协程中使用client.start()
,而不是在单独的线程中使用client.run()
。
更详细的示例在这里