C-Python asyncio:在线程中运行 discord.py [英] C-Python asyncio: running discord.py in a thread

查看:99
本文介绍了C-Python asyncio:在线程中运行 discord.py的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 代码:

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" 线程/代码>方法.我不明白这个.

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.

推荐答案

回答我自己的问题:

我应该感谢这个来源 asyncio-you-are-一个复杂的野兽,我终于找到了解决方案.
最终的工作代码如下所示:

I should thank this source asyncio-you-are-a-complex-beast where I finally found a solution.
The final working code looks like this:

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 并读取了 0.16.12 的文档,而我不得不查看 discord.py.rewrite (例如在 on_message 里面我用错了 client.send_message 而我不得不使用 message.channel.send)

My main mistake was that for the game I compiled and used the latest rewrite version while inside the system over the pip I got 0.16.12 and read the documentation for 0.16.12 while I had to look at discord.py.rewrite (for example inside on_message I used wrong client.send_message while I had to use message.channel.send)

这篇关于C-Python asyncio:在线程中运行 discord.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆