Questions tagged [coroutine]

Coroutines are a general control structure whereby flow control is cooperatively passed between two different routines without returning.

A coroutine is a pair of code sequences that can interchange control.

In hardware this is accomplished by interchanging the stack top and the program counter. Co-routines were extensively used inside operating systems.

Coroutines are well-suited for creating iterators and pipes. Subroutines can return only once; in contrast, coroutines can return (yield) several times. Yielding returns the result to the calling coroutine and gives it back control, like a usual subroutine. However, the next time the coroutine is called, the execution starts just after the yield call.

Related tags

1721 questions
11167
votes
38 answers

What does the "yield" keyword do?

What is the use of the yield keyword in Python, and what does it do? For example, I'm trying to understand this code1: def _get_child_candidates(self, distance, min_dist, max_dist): if self._leftchild and distance - max_dist < self._median: …
Alex. S.
  • 126,349
  • 16
  • 50
  • 61
233
votes
11 answers

What is a coroutine?

What is a coroutine? How are they related to concurrency?
yesraaj
  • 42,284
  • 65
  • 185
  • 246
206
votes
6 answers

Difference between a "coroutine" and a "thread"?

What are the differences between a "coroutine" and a "thread"?
jldupont
  • 82,560
  • 49
  • 190
  • 305
204
votes
8 answers

What is the difference between launch/join and async/await in Kotlin coroutines

In the kotlinx.coroutines library you can start new coroutine using either launch (with join) or async (with await). What is the difference between them?
Roman Elizarov
  • 20,708
  • 8
  • 52
  • 54
200
votes
9 answers

What is the difference between a thread and a fiber?

What is the difference between a thread and a fiber? I've heard of fibers from ruby and I've read heard they're available in other languages, could somebody explain to me in simple terms what is the difference between a thread and a fiber.
tatsuhirosatou
  • 22,689
  • 14
  • 37
  • 38
150
votes
3 answers

Coroutine vs Continuation vs Generator

What is the difference between a coroutine and a continuation and a generator ?
Mehdi Asgari
  • 1,931
  • 3
  • 15
  • 17
143
votes
4 answers

Greenlet Vs. Threads

I am new to gevents and greenlets. I found some good documentation on how to work with them, but none gave me justification on how and when I should use greenlets! What are they really good at? Is it a good idea to use them in a proxy server or…
Rsh
  • 5,722
  • 5
  • 32
  • 43
143
votes
7 answers

How does StartCoroutine / yield return pattern really work in Unity?

I understand the principle of coroutines. I know how to get the standard StartCoroutine / yield return pattern to work in C# in Unity, e.g. invoke a method returning IEnumerator via StartCoroutine and in that method do something, do yield return…
Ghopper21
  • 9,299
  • 7
  • 53
  • 83
128
votes
12 answers

Equivalent C++ to Python generator pattern

I've got some example Python code that I need to mimic in C++. I do not require any specific solution (such as co-routine based yield solutions, although they would be acceptable answers as well), I simply need to reproduce the semantics in some…
Noah Watkins
  • 5,046
  • 3
  • 29
  • 42
117
votes
3 answers

What are coroutines in C++20?

What are coroutines in c++20? In what ways it is different from "Parallelism2" or/and "Concurrency2" (look into below image)? The below image is from ISOCPP. https://isocpp.org/files/img/wg21-timeline-2017-03.png
Pavan Chandaka
  • 8,891
  • 4
  • 17
  • 29
108
votes
4 answers

asyncio.ensure_future vs. BaseEventLoop.create_task vs. simple coroutine?

I've seen several basic Python 3.5 tutorials on asyncio doing the same operation in various flavours. In this code: import asyncio async def doit(i): print("Start %d" % i) await asyncio.sleep(3) print("End %d" % i) return i if…
crusaderky
  • 2,131
  • 2
  • 15
  • 24
85
votes
1 answer

Can "experimental" Kotlin coroutines be used in production?

Can Kotlin coroutines be used in production, and what does their experimental status mean?
Roman Elizarov
  • 20,708
  • 8
  • 52
  • 54
70
votes
6 answers

The Pause monad

Monads can do many amazing, crazy things. They can create variables which hold a superposition of values. They can allow you to access data from the future before you compute it. They can allow you to write destructive updates, but not really. And…
MathematicalOrchid
  • 58,942
  • 16
  • 110
  • 206
69
votes
2 answers

How do stackless coroutines differ from stackful coroutines?

Background: I'm asking this because I currently have an application with many (hundreds to thousands) of threads. Most of those threads are idle a great portion of the time, waiting on work items to be placed in a queue. When a work item comes…
Jason R
  • 10,110
  • 5
  • 44
  • 72
69
votes
3 answers

In python is there a way to check if a function is a "generator function" before calling it?

Lets say I have two functions: def foo(): return 'foo' def bar(): yield 'bar' The first one is a normal function, and the second is a generator function. Now I want to write something like this: def run(func): if…
Carlos
  • 882
  • 7
  • 9
1
2 3
99 100