带有异步定义的 Python [无效语法]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43948454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:33:34  来源:igfitidea点击:

Python [Invalid syntax] with async def

pythonsyntaxasync-awaitdiscorddiscord.py

提问by Viewerisland

I am trying write discord bots using Python, I have come across and threw together this bot.

我正在尝试使用 Python 编写不和谐的机器人,我遇到了这个机器人并将其放在一起。

import discord
import asyncio
import random

client = discord.Client()
inEmail = input("Email:")
inPassword = input("Passwd:")

async def background_loop():
    await client.wait_until_ready()
    while not client.is_closed:
        channel = client.get_channel("************")
        messages = ["Hello!", "How are you doing?", "Testing!!"]
        await client.send_message(channel, random.choice(messages))
        await asyncio.sleep(120)

client.loop.create_task(background_loop())
client.run(inEmail, inPassword)

Yet when I tried to run it, I received a SyntaxError:

然而,当我尝试运行它时,我收到了一个SyntaxError

File "1.py", line 7
  async def background_loop():
     ^
SyntaxError: invalid syntax

Why is that? I have never received that before when I tested it.

这是为什么?在我测试它之前,我从未收到过。

回答by abccd

Asynchronous requests were introduced to Python in v3.3, if you're running Python prior to v3.3 (including v2.X), you'll have to install a newer version of Python.

异步请求是在 v3.3 中引入 Python 的,如果您在 v3.3(包括 v2.X)之前运行 Python,则必须安装较新版本的 Python。



Onlyif you are running Python 3.3: asynciois not part of the stdlib, you'll need to install it manually from pypi:

当您运行 Python 3.3:asyncio不是 stdlib 的一部分时,您才需要从 pypi 手动安装它

pip install asyncio

The asyncand awaitkeywords are only valid for Python 3.5 or newer. If you're using Python 3.3 or 3.4, you will need to make the following changes to your code:

asyncawait关键字是唯一有效的Python 3.5或更高版本。如果您使用的是 Python 3.3 或 3.4,则需要对代码进行以下更改:

  1. Use the @asyncio.coroutinedecorator instead of the asyncstatement:
  1. 使用@asyncio.coroutine装饰器而不是async语句:

async def func():
    pass

# replace to:

@asyncio.coroutine
def func():
    pass
  1. Use yield frominstead of await:
  1. 使用yield from代替await

await coroutine() 

# replace to:

yield from coroutine()

Here is an example of what your function need to change into (if you're on 3.3-3.4):

以下是您的函数需要更改为的示例(如果您使用的是 3.3-3.4):

import asyncio

@asyncio.coroutine 
def background_loop():
    yield from client.wait_until_ready()
    while not client.is_closed:
        channel = client.get_channel("************")
        messages = ["Hello!", "How are you doing?", "Testing!!"]
        yield from client.send_message(channel, random.choice(messages))
        yield from asyncio.sleep(120)

The aforementioned syntax is still supported in newer versions of Python 3, but it is recommended to use awaitand asyncif there's no need to support for Python 3.3-3.4. You can refer back to this documentation, here's a short snippet:

较新版本的 Python 3 仍然支持上述语法,但建议使用awaitasync如果不需要支持 Python 3.3-3.4。你可以参考这个文档,这里有一个简短的片段:

The async deftype of coroutine was added in Python 3.5, and is recommended if there is no need to support older Python versions.

async def协程的类型是在Python 3.5中添加的,如果不需要支持旧版本的Python,推荐使用。



Aside:

在旁边:

discord.pycurrently supports 3.4.2-3.6.6, (It does not support 3.3-3.4.1, 3.7 as of January 2019).

discord.py目前支持3.4.2-3.6.6,(它不支持3.3-3.4.1,3.7为2019一月)。

For developing with discord.py, I suggest using the discord.py rewrite branch:

对于使用 discord.py 进行开发,我建议使用 discord.py 重写分支:

discord.py-rewritesupports 3.5.3-3.7.

discord.py -rewrite支持 3.5.3-3.7。

回答by Er M S Dandyan

From version 3.7 asyncand awaitare reserved keywords

从 3.7 版开始 asyncawait是保留关键字

like the error in below image.

就像下图中的错误。

enter image description here

在此处输入图片说明

Copy and open the path (without __init__.py). You will get a list of .py files enter image description here

复制并打开路径(不带__init__.py)。您将获得 .py 文件列表 在此处输入图片说明

Rename async.pyto _async.pyor anything you want, as async is now a reserved keyword with us from version 3.7.

重命名async.py_async.py或任何您想要的名称,因为 async 现在是我们从 3.7 版开始的保留关键字。

Once renamed, modify the new name everywhere.

重命名后,随处修改新名称。

*NOTE Although it is not a permanent solution but it worked for me in case of the same syntax error while working with firebase. Best solution is to go with previous version of Python. i.e version below 3.7.

*注意虽然它不是一个永久性的解决方案,但它对我有用,以防在使用 firebase 时出现相同的语法错误。最好的解决方案是使用以前版本的 Python。即低于 3.7 的版本。

回答by Marcelo Tournier

I solved it by installing the updated PyMC from github (they corrected the bug that happens in Python 3.7):

我通过从 github 安装更新的 PyMC 解决了它(他们更正了 Python 3.7 中发生的错误):

pip install git+https://github.com/pymc-devs/pymc.git

pip install git+https://github.com/pymc-devs/pymc.git