coroutine

Equivalent C++ to Python generator pattern

久未见 提交于 2019-11-27 06:02:45
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 manner. Python This is a basic sequence generator, clearly too large to store a materialized version. def pair_sequence(): for i in range(2**32): for j in range(2**32): yield (i, j) The goal is to maintain two instances of the sequence above, and iterate over them in semi-lockstep, but in chunks. In the example below the first_pass uses the sequence of

What is a coroutine?

a 夏天 提交于 2019-11-27 05:49:26
What is a coroutine? How are they related to concurrency? Coroutines and concurrency are largely orthogonal. Coroutines are a general control structure whereby flow control is cooperatively passed between two different routines without returning. The 'yield' statement in Python is a good example. It creates a coroutine. When the 'yield ' is encountered the current state of the function is saved and control is returned to the calling function. The calling function can then transfer execution back to the yielding function and its state will be restored to the point where the 'yield' was

What is the difference between a thread and a fiber?

倾然丶 夕夏残阳落幕 提交于 2019-11-27 04:58:53
问题 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. 回答1: In the most simple terms, threads are generally considered to be preemptive (although this may not always be true, depending on the operating system) while fibers are considered to be light-weight, cooperative threads. Both are separate execution paths

Turn functions with a callback into Python generators?

巧了我就是萌 提交于 2019-11-27 04:12:48
The Scipy minimization function (just to use as an example), has the option of adding a callback function at each step. So I can do something like, def my_callback(x): print x scipy.optimize.fmin(func, x0, callback=my_callback) Is there a way to use the callback function to create a generator version of fmin, so that I could do, for x in my_fmin(func,x0): print x It seems like it might be possible with some combination of yields and sends, but I can quite think of anything. As pointed in the comments, you could do it in a new thread, using Queue . The drawback is that you'd still need some way

Unity - IEnumerator's yield return null

房东的猫 提交于 2019-11-27 03:27:24
问题 I'm currently trying to understand IEnumerator & Coroutine within the context of Unity and am not too confident on what the "yield return null" performs. At the moment i believe it basically pauses and waits for the next frame and in the next frame it'll go back to perform the while statement again. If i leave out the "yield return null" it seems the object will instantly move to its destination or perhaps "skip a lot of frames". So i guess my question is how does this "yield return null"

Available Coroutine Libraries in Java

最后都变了- 提交于 2019-11-27 02:46:14
I would like to do some stuff in Java that would be clearer if written using concurrent routines, but for which full-on threads are serious overkill. The answer, of course, is the use of coroutines , but there doesn't appear to be any coroutine support in the standard Java libraries and a quick Google on it brings up tantalising hints here or there, but nothing substantial. Here's what I've found so far: JSIM has a coroutine class, but it looks pretty heavyweight and conflates, seemingly, with threads at points. The point of this is to reduce the complexity of full-on threading, not to add to

PEP 0492 - Python 3.5 async keyword

佐手、 提交于 2019-11-27 00:21:43
问题 PEP 0492 adds the async keyword to Python 3.5. How does Python benefit from the use of this operator? The example that is given for a coroutine is async def read_data(db): data = await db.fetch('SELECT ...') According to the docs this achieves suspend[ing] execution of read_data coroutine until db.fetch awaitable completes and returns the result data. Does this async keyword actually involve creation of new threads or perhaps the use of an existing reserved async thread? In the event that

How are generators and coroutines implemented in CPython?

佐手、 提交于 2019-11-27 00:06:39
问题 I've read that in CPython, the interpreter stack (the list of Python functions called to reach this point) is mixed with the C stack (the list of C functions that were called in the interpreter's own code). If so, then how are generators and coroutines implemented? How do they remember their execution state? Does CPython copy each generator's / coroutine's stack to and from an OS stack? Or does CPython simply keep the generator's topmost stack frame on the heap, since the generator can only

Can “experimental” Kotlin coroutines be used in production?

你离开我真会死。 提交于 2019-11-26 22:42:46
问题 Can Kotlin coroutines be used in production, and what does their experimental status mean? 回答1: UPDATE : Kotlin coroutines are no longer experimental as of Kotlin 1.3. Kotlin coroutines can and should be used in production. That was the chief reason to officially release them in Kotlin 1.1. Having released them, the JetBrains team had committed to maintain backwards compatibility with respect to any changes that are introduced to them in the minor releases as they evolve, while allowing

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

北慕城南 提交于 2019-11-26 22:30:09
问题 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? 回答1: launch is used to fire and forget coroutine . It is like starting a new thread. If the code inside the launch terminates with exception, then it is treated like uncaught exception in a thread -- usually printed to stderr in backend JVM applications and crashes Android applications. join is used to wait for completion of the launched