Fra*_*ank 2 python async-await python-asyncio discord.py
我有一个永远持续运行的脚本(它检查文件中的更改)。每当制作奇怪的文件时,我都需要发送 Discord 消息。
def run(self):如下)来自子类,所以我无法将其更改为async def run(self):. 因此我不能使用await channel.send()run_coroutine_threadsafe: https: //stackoverflow.com/a/53726266/9283107。效果很好!但问题是,消息被放入队列中,并且在该脚本完成之前永远不会发送(在我的情况下是:永远不会)。我假设发送消息函数被放入该脚本所在的线程中,因此线程永远不会到达它们?也许我们可以将其放入run_coroutine_threadsafe一个单独的线程或其他东西中?这是我能做的最小的例子,它仍然显示了我的子类问题。
import discord
import os
import asyncio
import time
# CHANNEL_ID = 7659170174????????
client = discord.Client()
channel = None
class Example():
# Imagine this run comes from a subclass, so you can't add sync to it!
def run(self):
# await channel.send('Test') # We can't do this because of the above comment
asyncio.run_coroutine_threadsafe(channel.send('Test'), _loop)
print('Message sent')
@client.event
async def on_ready():
print('Discord ready')
global channel
channel = client.get_channel(CHANNEL_ID)
for i in range(2):
Example().run()
time.sleep(3)
print('Discord messages should appear by now. Sleeping for 20s to give it time (technically this would be infinite)')
time.sleep(20)
print('Script done. Now they only get sent for some reason')
_loop = asyncio.get_event_loop()
client.run('Your secret token')
Run Code Online (Sandbox Code Playgroud)
首先,请注意,您不允许调用阻止代码,例如time.sleep()从async def. 要启动阻塞函数并让它与 asyncio 通信,您可以从on_ready顶层甚至从顶层生成一个后台线程,如下所示:
# checker_function is the function that blocks and that
# will invoke Example.run() in a loop.
threading.Thread(
target=checker_function,
args=(asyncio.get_event_loop(), channel)
).start()
Run Code Online (Sandbox Code Playgroud)
您的主线程将运行 asyncio 事件循环,您的后台线程将检查文件,用于asyncio.run_coroutine_threadsafe()与 asyncio 和不和谐进行通信。
正如您链接到的答案下的评论所指出的,asyncio.run_coroutine_threadsafe假设您有多个线程正在运行(因此“线程安全”),其中一个线程运行事件循环。在您实现这一点之前,任何使用尝试asyncio.run_coroutine_threadsafe都将失败。
| 归档时间: |
|
| 查看次数: |
2658 次 |
| 最近记录: |