首页 > 编程 > Python > 正文

在python里从协程返回一个值的示例

2019-11-25 13:15:47
字体:
来源:转载
供稿:网友

下面的例子演法了怎么样从协程里返回一个值:

import asyncio async def coroutine():  print('in coroutine')  return 'result' event_loop = asyncio.get_event_loop()try:  return_value = event_loop.run_until_complete(    coroutine()  )  print('it returned: {!r}'.format(return_value))finally:  event_loop.close()

结果输出如下:

in coroutineit returned: 'result'

在这个例子里,通过asyncio库方法get_event_loop()来获得事件循环对象,然后调用run_until_complete()方法来执行协程到结束。

以上这篇在python里从协程返回一个值的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表