在python里从协程返回一个值的示例
下面的例子演法了怎么样从协程里返回一个值:
importasyncio asyncdefcoroutine(): print('incoroutine') return'result' event_loop=asyncio.get_event_loop() try: return_value=event_loop.run_until_complete( coroutine() ) print('itreturned:{!r}'.format(return_value)) finally: event_loop.close()
结果输出如下:
incoroutine itreturned:'result'
在这个例子里,通过asyncio库方法get_event_loop()来获得事件循环对象,然后调用run_until_complete()方法来执行协程到结束。
以上这篇在python里从协程返回一个值的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。