Yeah, they are almost the same.
@gen.coroutine is from old days when Python didn’t have
async/await keywords. So
@gen.coroutine was used for turning regular functions (or generators) into asynchronous generators.
For newer python versions (3.5+), the
async/await syntax should be preferred over
Keep these things in mind while converting the functions:
- Don’t decorate
async deffunctions with
@gen.coroutine(like you’ve done in your second code example).
yield Nonestatement works but
- You can
yielda list (such as
yield [f1(), f2()]), but with
await gen.multi(f1(), f2())or
await asyncio.gather(f1(), f2()). (Thanks to Ben for mentioning this.)
CLICK HERE to find out more related problems solutions.