- 此bounty已结束**。此问题的答案可获得+50的信誉奖励。奖励宽限期将在1小时后结束。NinjaWarrior正在寻找来自信誉良好来源的答案:答案应该通过修改我提供的代码片段以及解释来提供代码示例。
我正尝试将一个 Dataframe 发送到Discord通道。但是,我在获取www.example.com以关闭连接并继续下一个任务时遇到问题。我已经尝试使用此线程(How to run async function in Airflow?)中建议的事件循环以及www.example.com()函数。我对异步不太熟悉,希望在此处获得一些指针。以下是我的Python代码,我将使用 Discord.py to close the connection and move on the next task. I've tried using the event loop as suggested in this thread ( How to run async function in Airflow? ) as well as asyncio.run() function. Not really familiar with the async and hoping to get some pointers here. Below is my code in Python that I've tried importing in DAG and Task without success. Thanks in advance!
空气流量:2.5.1
Python语言:3.7
import discord
from tabulate import tabulate
import asyncio
import pandas as pd
async def post_to_discord(df, channel_id, bot_token, as_message=True, num_rows=5):
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
try:
@client.event
async def on_ready():
channel = client.get_channel(channel_id)
if as_message:
# Post the dataframe as a message, num_rows rows at a time
for i in range(0, len(df), num_rows):
message = tabulate(df.iloc[i:i+num_rows,:], headers='keys', tablefmt='pipe', showindex=False)
await channel.send(message)
else:
# Send the dataframe as a CSV file
df.to_csv("dataframe.csv", index=False)
with open("dataframe.csv", "rb") as f:
await channel.send(file=discord.File(f))
# client.run(bot_token)
await client.start(bot_token)
await client.wait_until_ready()
finally:
await client.close()
async def main(df, channel_id, bot_token, as_message=True, num_rows=5):
# loop = asyncio.get_event_loop()
# result = loop.run_until_complete(post_to_discord(df, channel_id, bot_token, as_message, num_rows))
result = asyncio.run(post_to_discord(df, channel_id, bot_token, as_message, num_rows))
await result
return result
if __name__ =='__main__':
main()
1条答案
按热度按时间fkvaft9z1#
看起来你的脚本可以工作,但是服务器阻塞了打开的套接字(值得称赞的是-discord服务器很擅长这个),所以我们将创建一个ping函数(从另一个答案中采用)。
请随意测试您的ID
你应该看到
然后让我们继续改进您的代码: