gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:41:21 25 4
gpt4 key购买 nike

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

C 代码:

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异常:

...
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

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

最佳答案

回答我自己的问题:

我应该感谢这个来源 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))

我的主要错误是,对于我编译的游戏并使用了最新的rewrite版本,而在系统内部通过pip我得到了0.16.12并读取了documentation for 0.16.12,而我必须查看 discord.py.rewrite (例如,在 on_message 中,我使用了错误的 client.send_message,而我必须使用 message.channel.send)

关于C-Python asyncio : running discord. py 在线程中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55030714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com