Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discordbot using threading raise "RuntimeError: set_wakeup_fd only works in main thread" only on linux

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)
like image 228
MonteCore Avatar asked Jul 01 '20 07:07

MonteCore


2 Answers

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

like image 61
MonteCore Avatar answered Oct 02 '22 10:10

MonteCore


I'd suggest you to use client.start() in async coroutine instead of client.run() in separate thread.

More detailed example here

like image 26
alex_noname Avatar answered Oct 02 '22 12:10

alex_noname