python - 如何在普通函数中等待函数
问题描述
在下面的代码中,我将通过 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)
解决方案
您将需要使用 anasyncio.Task
来运行协程。
推荐阅读
- html - 无法从父组件在子组件中打开表单 - “无法读取属性 xxx”
- java - 可终结对象如何至少需要 2 个垃圾回收周期才能被回收?
- java - Java:无法理解如何从其他类调用实例
- javascript - 窗口对象中是否有 string.find() 的等价物?
- python - 使用 django rest 框架将数据保存到 Postgres DB
- elasticsearch - 在弹性搜索中匹配名称
- java - 如何通过@RestControllerAdvice 处理@Valid MethodArgumentNotValidException
- c++ - 如何在不更改列表的情况下订购数字列表
- algorithm - 最短不常见子序列
- android - Android BroadcastReceiver执行