C-Python asyncio:在线程中运行 discord.py

Igo*_*orZ 7 c asynchronous python-3.x python-asyncio discord.py

我必须在单独的线程中启动 discord.py,因为我无法阻止我的主线程。
这是一个游戏服务器C/Python 3.7 (ubuntu 18)

代码:

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, "");
}
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

set_wakeup_fd 异常:

...
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
Run Code Online (Sandbox Code Playgroud)

我应该提到我在 python 上尝试了相同的代码(没有 C 代码)并且它有效。
此错误告诉我有关主线程的信息。但是我并没有sv_discord在新线程内部创建,从日志中可以看出,它是"Main"线程内部init()方法。我不明白这个。

Igo*_*orZ 10

回答我自己的问题:

我应该感谢这个源asyncio-you-are-a-complex-beast我终于找到了解决方案。
最终的工作代码如下所示:

import discord
import asyncio
from threading import Thread


client = discord.Client()


def init():
    loop = asyncio.get_event_loop()
    loop.create_task(client.start(TOKEN))
    Thread(target=loop.run_forever).start()


@client.event
async def on_message(message):
    if message.author == client.user:
        return

    print("on_message content: %s, channel: %s" % (message.content, message.channel))
    await message.channel.send('Hello!')


@client.event
    async def on_ready():
    print("Discord bot logged in as: %s, %s" % (client.user.name, client.user.id))
Run Code Online (Sandbox Code Playgroud)

我的主要错误是,对这场比赛我编译和使用了最新的rewrite版本,而该系统在PIP我里面0.16.12并阅读文档0.16.12,而我不得不看discord.py.rewrite(例如里面on_message我用错了client.send_message虽然我不得不使用message.channel.send

  • 对我来说,关键的一点是从 `client.run` 切换到 `client.start` 并将循环设置更改为 `loop.create_task(start())` + `loop.run_forever`。正如您所指出的,我没有使用“get_child_watcher”,并且代码仍然运行得很好。感谢您为我指明了正确的方向! (2认同)