首页 > 解决方案 > 如何在普通函数中等待函数

问题描述

在下面的代码中,我将通过 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)

标签: pythonasync-awaitdiscord.pytweepy

解决方案


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


推荐阅读