【问题标题】:How to await a function in a normal function如何在普通函数中等待函数
【发布时间】:2021-08-21 22:26:56
【问题描述】:

在以下代码中,我将通过 Tweepy 从 Twitter API 接收推文。我想在 Discord 频道中发送这些推文,但是如何在普通类中使用 await 函数。

import discord
import tweepy 

class Client(discord.Client):
    async def on_ready(self):
        print('ready')

class TweepyStream(tweepy.Stream):
    def on_connect(self):
        print('connceted')

    def on_status(self, status):
        print(status.text)
        #How can I here send a message into a discord channel ?


stream = TweepyStream(keys)
stream.filter(follow = [],  threaded=True)

client = Client()
client.run(token)

【问题讨论】:

  • 我会亲自使用bot.dispatch() 发送一个事件,然后做你必须做的事情

标签: python async-await discord.py tweepy


【解决方案1】:

您需要使用asyncio.Task 来运行协程。

【讨论】:

  • “使用 X”更多的是评论。如果您要回答问题,则应包含一个示例或一些上下文,以便您的帖子完全回答问题。
  • 但是当我想发送一条消息'await channel.send(message)'它不起作用它给我错误:'RuntimeError: Timeout context manager should be used inside a task' 我该怎么办现在必须做什么?
  • 你在使用新的事件循环吗?您应该使用现有的事件循环。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-23
  • 1970-01-01
  • 2016-10-29
  • 2018-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多