I am using the threading module to host a web server and a Discord bot at the same time. Everything runs fine on Windows but as soon as I load it onto my Linux server I get the following error:
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
I have upgraded from python 3.7 up to python 3.8 but I still have the same error.
Here is my code: main.py (webserver worked)
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()
Ok now it start the webserver and the bot. But when I try to do something after the bot start nothing happens. However, I am interested in why this is so. If someone knows a good and extensive contribution like books on threading, please send it
I'd suggest you to use client.start()
in async coroutine instead of client.run()
in separate thread.
More detailed example here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With