Yield from coroutine vs yield from task

前端 未结 3 1364
孤街浪徒
孤街浪徒 2020-12-13 19:31

Guido van Rossum, in his speech in 2014 on Tulip/Asyncio shows the slide:

Tasks vs coroutines

  • Compare:

3条回答
  •  北海茫月
    2020-12-13 20:17

    For the calling side co-routine yield from coroutine() feels like a function call (i.e. it will again gain control when coroutine() finishes).

    yield from Task(coroutine()) on the other hand feels more like creating a new thread. Task() returns almost instantly and very likely the caller gains control back before the coroutine() finishes.

    The difference between f() and th = threading.Thread(target=f, args=()); th.start(); th.join() is obvious, right?

提交回复
热议问题