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

1949 questions
47 answers

What does the "yield" keyword do?

What is the use of the yield keyword in Python? 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.
  • 134,175
  • 17
  • 51
  • 61
13 answers

What is a coroutine?

What is a coroutine? How are they related to concurrency?
  • 43,882
  • 65
  • 187
  • 248
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
  • 23,507
  • 10
  • 56
  • 59
6 answers

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

What are the differences between a "coroutine" and a "thread"?
  • 86,720
  • 54
  • 193
  • 309
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.
  • 23,729
  • 14
  • 38
  • 39
3 answers

Coroutine vs Continuation vs Generator

What is the difference between a coroutine and a continuation and a generator ?
Mehdi Asgari
  • 2,001
  • 3
  • 16
  • 17
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…
  • 6,352
  • 5
  • 34
  • 44
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…
  • 9,675
  • 8
  • 56
  • 85
13 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,196
  • 3
  • 30
  • 44
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.
Pavan Chandaka
  • 9,812
  • 5
  • 21
  • 31
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…
  • 2,263
  • 2
  • 17
  • 25
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
  • 23,507
  • 10
  • 56
  • 59
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,570
  • 5
  • 46
  • 73
19 answers

How do you implement Coroutines in C++

I doubt it can be done portably, but are there any solutions out there? I think it could be done by creating an alternate stack and reseting SP,BP, and IP on function entry, and having yield save IP and restore SP+BP. Destructors and exception…
Mike Elkins
  • 1,328
  • 1
  • 10
  • 12
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…
  • 60,472
  • 17
  • 117
  • 211
2 3
99 100