【问题标题】:C-Python asyncio: running discord.py in a threadC-Python asyncio:在线程中运行 discord.py
【发布时间】:2019-07-28 13:37:29
【问题描述】:

我必须在单独的线程中启动 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,从日志中可以看出,它是init()方法中的"Main"线程。我不明白这个。

【问题讨论】:

    标签: c python-3.x asynchronous python-asyncio discord.py


    【解决方案1】:

    回答我自己的问题:

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

    我的主要错误是,我在系统内部通过 pip 编译并使用了最新的rewrite 版本的游戏,我得到了0.16.12 并阅读了documentation for 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 并且代码仍然运行良好。感谢您为我指明正确的方向!
    • @computerfreaker 这是我为服务器制作的模块的工作副本:github。
    【解决方案2】:

    我有一个类似的边缘情况(不是 asyncio 的真正预期用途,而是什么鬼)我需要线程 discord.py 实例。

    我最终使用以下内容修改了您的解决方案:

    class discordHost(discord.Client):
        async def on_ready(self):
            print(f'{DThread.discord_client.user} has connected.')
    
    class Threader(Thread):
        def __init__(self):
            Thread.__init__(self)
            self.loop = asyncio.get_event_loop()
            self.start()
    
        async def starter(self):
            self.discord_client = discordHost()
            await self.discord_client.start(DISCORD_TOKEN)
    
        def run(self):
            self.name = 'Discord.py'
    
            self.loop.create_task(self.starter())
            self.loop.run_forever()
    
    if not 'Discord.py' in [t.name for t in tenumerate()]:
        DThread = Threader()
    

    这或多或少做同样的事情,但我已将Thread 和discord.Client 子类化,以便更轻松地处理两者的结果。

    这里唯一需要注意的是,asyncio.get_event_loop() 必须在 Thread.start() 之前调用,否则它会混淆并丢失它的主线程上下文。

    【讨论】:

    • 如何从另一个持有 DThread 对象的线程向客户端发送命令?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 2014-04-21
    相关资源
    最近更新 更多