I try to run a coroutine.i write a correct demo:
import asyncio async def outer(): print('in outer') print('waiting for result1') result1 = await phase1() print('waiting for result2') result2 = await phase2(result1) return (result1, result2) async def phase1(): print('in phase1') return 'result1' async def phase2(arg): print('in phase2') return 'result2 derived from {}'.format(arg) event_loop = asyncio.get_event_loop() try: return_value = event_loop.run_until_complete(outer()) print('return value: {!r}'.format(return_value)) finally: event_loop.close() I want to know what if the outer function is not a coroutine, so, I remove async, and after that:
import asyncio def outer(): print('in outer') print('waiting for result1') result1 = yield from phase1() print('waiting for result2') result2 = yield from phase2(result1) return (result1, result2) async def phase1(): print('in phase1') return 'result1' async def phase2(arg): print('in phase2') return 'result2 derived from {}'.format(arg) event_loop = asyncio.get_event_loop() try: return_value = event_loop.run_until_complete(outer()) print('return value: {!r}'.format(return_value)) finally: event_loop.close() then i run this event loop ,I find this error.how to explain this error?it not allowed to use a usual function to call coroutine? I used to thought yield from can be used in a usual funciton,but here,absolutly,it could't.who can tell me the reason?
async defsimply isn't a generator, so you can'tyield fromit. You can extract a generator from it and yield from that, e.g.yield from phase1().__await__(), but I can't see a reason why you would ever want to.