Skip to main content
added 55 characters in body
Source Link
IgorZ
  • 1.2k
  • 3
  • 23
  • 53

I have to launch discord.py in a separate thread since I can't block my main thread.
It is a game server C/Python 3.7 (ubuntu 18)

C code:

int pysDiscord_Init;
...
PyObject *psv_discord;

psv_discord = Python_LoadModule("sv_discord");
if (psv_discord != NULL) {
  pysDiscord_Init = Python_RegisterFunction(psv_discord, "sv_discord", "init");
  Python_Execute(pysDiscord_Init, "");
}

sv_discord.py

import discord
import asyncio
import threading
from concurrent.futures import ThreadPoolExecutor
import multiprocessing

TOKEN = '12345'

client = discord.Client()

def init():
    print("Initializing Discord...")
    print("current_thread: %s" % threading.current_thread())
    t = threading.Thread(target=client.run, args=(TOKEN,))
    t.start()

or

def init():
    print("Initializing Discord...")
    print("current_thread: %s" % threading.current_thread())
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    asyncio.get_child_watcher().attach_loop(loop)
    pool = ThreadPoolExecutor(max_workers=multiprocessing.cpu_count())
    task = loop.run_in_executor(pool, client.run, TOKEN)
    loop.run_until_complete(task)

set_wakeup_fd exception:

...
Initializing Discord...
current_thread: <_MainThread(MainThread, started 4150019840)>

Exception in thread Thread-1:
 Traceback (most recent call last):
 File "./build/Lib/asyncio/unix_events.py", line 92, in add_signal_handler
 ValueError: set_wakeup_fd only works in main thread

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
 File "./build/Lib/threading.py", line 917, in _bootstrap_inner
 File "./build/Lib/threading.py", line 865, in run
 File "./../source/discord.py-rewrite/discord/client.py", line 550, in run
 File "./build/Lib/asyncio/unix_events.py", line 94, in add_signal_handler
RuntimeError: set_wakeup_fd only works in main thread

I should mention that I tried the same code on the python (without the C code) and it works.
This error tells me about the main thread. But I don't create sv_discord inside the new thread, and as you can see from the log, it is the "Main" thread inside init() method. I don't understand this.

I have to launch discord.py in a separate thread since I can't block my main thread.
It is a game server C/Python 3.7 (ubuntu 18)

C code:

int pysDiscord_Init;
...
PyObject *psv_discord;

psv_discord = Python_LoadModule("sv_discord");
if (psv_discord != NULL) {
  pysDiscord_Init = Python_RegisterFunction(psv_discord, "sv_discord", "init");
  Python_Execute(pysDiscord_Init, "");
}

sv_discord.py

import discord
import asyncio
import threading

TOKEN = '12345'

client = discord.Client()

def init():
    print("Initializing Discord...")
    print("current_thread: %s" % threading.current_thread())
    t = threading.Thread(target=client.run, args=(TOKEN,))
    t.start()

or

def init():
    print("Initializing Discord...")
    print("current_thread: %s" % threading.current_thread())
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    asyncio.get_child_watcher().attach_loop(loop)
    pool = ThreadPoolExecutor(max_workers=multiprocessing.cpu_count())
    task = loop.run_in_executor(pool, client.run, TOKEN)
    loop.run_until_complete(task)

set_wakeup_fd exception:

...
Initializing Discord...
current_thread: <_MainThread(MainThread, started 4150019840)>

Exception in thread Thread-1:
 Traceback (most recent call last):
 File "./build/Lib/asyncio/unix_events.py", line 92, in add_signal_handler
 ValueError: set_wakeup_fd only works in main thread

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
 File "./build/Lib/threading.py", line 917, in _bootstrap_inner
 File "./build/Lib/threading.py", line 865, in run
 File "./../source/discord.py-rewrite/discord/client.py", line 550, in run
 File "./build/Lib/asyncio/unix_events.py", line 94, in add_signal_handler
RuntimeError: set_wakeup_fd only works in main thread

I should mention that I tried the same code on the python (without the C code) and it works.
This error tells me about the main thread. But I don't create sv_discord inside the new thread, and as you can see from the log, it is the "Main" thread inside init() method. I don't understand this.

I have to launch discord.py in a separate thread since I can't block my main thread.
It is a game server C/Python 3.7 (ubuntu 18)

C code:

int pysDiscord_Init;
...
PyObject *psv_discord;

psv_discord = Python_LoadModule("sv_discord");
if (psv_discord != NULL) {
  pysDiscord_Init = Python_RegisterFunction(psv_discord, "sv_discord", "init");
  Python_Execute(pysDiscord_Init, "");
}

sv_discord.py

import discord
import asyncio
import threading
from concurrent.futures import ThreadPoolExecutor
import multiprocessing

TOKEN = '12345'

client = discord.Client()

def init():
    print("Initializing Discord...")
    print("current_thread: %s" % threading.current_thread())
    t = threading.Thread(target=client.run, args=(TOKEN,))
    t.start()

or

def init():
    print("Initializing Discord...")
    print("current_thread: %s" % threading.current_thread())
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    asyncio.get_child_watcher().attach_loop(loop)
    pool = ThreadPoolExecutor(max_workers=multiprocessing.cpu_count())
    task = loop.run_in_executor(pool, client.run, TOKEN)
    loop.run_until_complete(task)

set_wakeup_fd exception:

...
Initializing Discord...
current_thread: <_MainThread(MainThread, started 4150019840)>

Exception in thread Thread-1:
 Traceback (most recent call last):
 File "./build/Lib/asyncio/unix_events.py", line 92, in add_signal_handler
 ValueError: set_wakeup_fd only works in main thread

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
 File "./build/Lib/threading.py", line 917, in _bootstrap_inner
 File "./build/Lib/threading.py", line 865, in run
 File "./../source/discord.py-rewrite/discord/client.py", line 550, in run
 File "./build/Lib/asyncio/unix_events.py", line 94, in add_signal_handler
RuntimeError: set_wakeup_fd only works in main thread

I should mention that I tried the same code on the python (without the C code) and it works.
This error tells me about the main thread. But I don't create sv_discord inside the new thread, and as you can see from the log, it is the "Main" thread inside init() method. I don't understand this.

edited tags
Link
IgorZ
  • 1.2k
  • 3
  • 23
  • 53
deleted 27 characters in body
Source Link
IgorZ
  • 1.2k
  • 3
  • 23
  • 53
import discord
import asyncio
import threading

TOKEN = '12345'

client = discord.Client()

def init():
    print("Initializing Discord...")
    print("current_thread: %s" % threading.current_thread())
    t = threading.Thread(target=client.run, args=(TOKEN,))
    t.start() 

or

def init():
    print("Initializing Discord...")
    print("current_thread: %s" % threading.current_thread())
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    asyncio.get_child_watcher().attach_loop(loop)
    pool = ThreadPoolExecutor(max_workers=multiprocessing.cpu_count())
    task = loop.run_in_executor(pool, client.run, TOKEN)
    loop.run_until_complete(task)

I should mention that I tried the same code on the python (without the C code) and it works.
This error tells me about the main thread. But I don't create sv_discord inside the new thread, and as you can see from the log, it is the "Main" thread inside init() method. I don't understand this.
Also I found this answer about adding asyncio.get_child_watcher() into the main thread. I tried to add all those 4 lines into the top of the init() but still got same set_wakeup_fd error.
Also I don't understand how to reproduce this error only with the python.

Additional:

asyncio.get_running_loop()
->
Python Error: Cannot read module file sv_discord!
no running event loop
import discord
import asyncio
import threading

TOKEN = '12345'

client = discord.Client()

def init():
    print("Initializing Discord...")
    print("current_thread: %s" % threading.current_thread())
    t = threading.Thread(target=client.run, args=(TOKEN,))
    t.start()

I should mention that I tried the same code on the python (without the C code) and it works.
This error tells me about the main thread. But I don't create sv_discord inside the new thread, and as you can see from the log, it is the "Main" thread inside init() method. I don't understand this.
Also I found this answer about adding asyncio.get_child_watcher() into the main thread. I tried to add all those 4 lines into the top of the init() but still got same set_wakeup_fd error.
Also I don't understand how to reproduce this error only with the python.

Additional:

asyncio.get_running_loop()
->
Python Error: Cannot read module file sv_discord!
no running event loop
import discord
import asyncio
import threading

TOKEN = '12345'

client = discord.Client()

def init():
    print("Initializing Discord...")
    print("current_thread: %s" % threading.current_thread())
    t = threading.Thread(target=client.run, args=(TOKEN,))
    t.start() 

or

def init():
    print("Initializing Discord...")
    print("current_thread: %s" % threading.current_thread())
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    asyncio.get_child_watcher().attach_loop(loop)
    pool = ThreadPoolExecutor(max_workers=multiprocessing.cpu_count())
    task = loop.run_in_executor(pool, client.run, TOKEN)
    loop.run_until_complete(task)

I should mention that I tried the same code on the python (without the C code) and it works.
This error tells me about the main thread. But I don't create sv_discord inside the new thread, and as you can see from the log, it is the "Main" thread inside init() method. I don't understand this.

added 143 characters in body
Source Link
IgorZ
  • 1.2k
  • 3
  • 23
  • 53
Loading
Source Link
IgorZ
  • 1.2k
  • 3
  • 23
  • 53
Loading