开始学习新的Python 3.5 Asyncio(协同程序)| Discord.py BOT崩溃的好地方

开始学习新的Python 3.5 Asyncio(协同程序)| Discord.py BOT崩溃的好地方,python,api,python-asyncio,coroutine,discord,Python,Api,Python Asyncio,Coroutine,Discord,因此,我似乎找不到任何关于在python中使用新的asyncio模块(async、await等)的好教程。此外,从我看过的所有教程来看,这个概念描述得很糟糕,我似乎无法完全理解协同程序的概念。我的意思是,这个概念背后的想法并不难,但没有一个地方可以让我确切地了解协同程序可以做什么和不能做什么,以及如何使用它们 例如,我为我目前正在构建的Discord机器人编写了一个名为YouTubeAPI的小类。Discord.py库的所有函数都使用asyncio,但我的类没有。我的类(YouTubeAPI)的

因此,我似乎找不到任何关于在python中使用新的asyncio模块(async、await等)的好教程。此外,从我看过的所有教程来看,这个概念描述得很糟糕,我似乎无法完全理解协同程序的概念。我的意思是,这个概念背后的想法并不难,但没有一个地方可以让我确切地了解协同程序可以做什么和不能做什么,以及如何使用它们

例如,我为我目前正在构建的Discord机器人编写了一个名为YouTubeAPI的小类。Discord.py库的所有函数都使用asyncio,但我的类没有。我的类(YouTubeAPI)的唯一目的是从YouTube数据API V3检索用户发布的最新视频的数据。事实上,我正在尝试构建一个机器人,让我了解有人发布的所有视频的最新情况

但是为了让机器人正常工作,我需要制作一个
update()
函数,定期获取所有视频,以便获取最新视频。问题是更新函数需要包装在
while True
循环(或类似的循环)中,这样我就可以使列表保持最新。如果我构建了一个无限循环,那么我会遇到机器人的问题(使机器人崩溃并且无法使用)

所以,我想也许我可以学习新的asyncio模块,并用这种方式解决问题。遗憾的是,我什么也没找到

下面是一些删除了所有API键的代码,这样您可以更容易地看到我的问题:

from Api_Test import YoutubeAPI
import discord
import asyncio

YoutubeName = 'Vsauce'
GOOGLE_API = 'API KEY'

print('Collecting YouTube Data.')
api = YoutubeAPI(GOOGLE_API, YoutubeName) # create object that will get all info for the name 'Vsauce'
print('YouTube Data collected succesfully.')
print('Starting bot.')

def getLastVideo():
    return api.videosData[0] # api.videosData looks like: [[title, link],[title, link],[title, link],]

client = discord.Client()

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')
    await client.send_message('Now testing: Last {} videos!'.format(YoutubeName))


#While Loop that keeps the api.videosData up-to-date and runs "await client.send_message('new video: title + ink')" if new video found in the list

client.run('Discord BOT token')
如果这篇文章的解释含糊不清,我非常抱歉,但我完全不知道如何使用asyncio或类似的东西,我发现自己几乎找不到关于这个新概念的文档。

你可以运行一个函数(比如从Youtube检索数据的东西)通过
asyncio。在后台确保未来

我自己的机器人的一个例子:

games = [
    'try :help',
    'with Atom.io',
    'with Python',
    'HuniePop'
]

async def randomGame():
    while True:
        await bot.change_presence(game=discord.Game(name=random.choice(games)))
        await asyncio.sleep(10*60) # 10 Minutes

有关这方面的更多信息,请参见:


但是您实际上在哪里运行client.run()函数呢?因为你不能在循环中运行它。这样你就可以让机器人崩溃。还是我错了


始终位于Discord.PY bot的最后一行,一旦函数发生,bot就会一直运行,直到客户端.close()函数发生或环境关闭。

您可以使用
确保未来()
循环时运行
。
在这里,循环在调用on_ready
时开始,并一直运行,直到bot关闭

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')
    await client.send_message('Now testing: Last {} videos!'.format(YoutubeName))

    asyncio.ensure_future(update_data(), client.loop) # Starts the infinite loop when the bot starts

async def update_data():
    while True:
        # Do the things you need to do in this loop
        await asyncio.sleep(1) # sleep for 1 second

client.run('Discord BOT token')

这可能会有帮助:。请尝试,​, 但是您实际上在哪里运行client.run()函数呢?因为你不能在循环中运行它。这样你就可以让机器人崩溃。还是我错了?
client.run("token")
@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')
    await client.send_message('Now testing: Last {} videos!'.format(YoutubeName))

    asyncio.ensure_future(update_data(), client.loop) # Starts the infinite loop when the bot starts

async def update_data():
    while True:
        # Do the things you need to do in this loop
        await asyncio.sleep(1) # sleep for 1 second

client.run('Discord BOT token')