异步函数Python 3.6.5 aiohttp中的'yield from'

编程入门 行业动态 更新时间:2024-10-28 01:23:17
本文介绍了异步函数Python 3.6.5 aiohttp中的'yield from'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

SyntaxError:异步函数内部的'yield from'

SyntaxError: 'yield from' inside async function

async def handle(request): for m in (yield from request.post()): print(m) return web.Response()

以前使用过python3.5,找到pep525,安装python3.6.5,仍然收到此错误。

Used python3.5 before, found pep525, install python3.6.5 and still receive this error.

推荐答案

您正在使用新的 async / await 语法来定义和执行协同例程,但尚未完成开关。您需要在此处使用 await

You are using the new async/await syntax to define and execute co-routines, but have not made a full switch. You need to use await here:

async def handle(request): post_data = await request.post() for m in post_data: print(m) return web.Response()

如果您想使用Python 3.5之前的旧语法,请使用 @ asyncio.coroutine 装饰器,删除 async 关键字,并使用 yield from 代替 await :

If you wanted to stick to the old, pre-Python 3.5 syntax, then mark your function as a coroutine with the @asyncio.coroutine decorator, drop the async keyword, and use yield from instead of await:

@async.coroutine def handle(request): post_data = yield from request.post() for m in post_data: print(m) return web.Response()

,但是此语法正在逐步淘汰,并且不像新语法那样容易发现和可读。仅在需要编写与旧版本Python兼容的代码时,才应使用此表单。

but this syntax is being phased out, and is not nearly as discoverable and readable as the new syntax. You should only use this form if you need to write code that is compatible with older Python versions.

更多推荐

异步函数Python 3.6.5 aiohttp中的'yield from'

本文发布于:2023-11-23 11:22:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621270.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   Python   yield   aiohttp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!