coroutine

Async/await as a replacement of coroutines

試著忘記壹切 提交于 2019-11-28 17:38:36
I use C# iterators as a replacement for coroutines, and it has been working great. I want to switch to async/await as I think the syntax is cleaner and it gives me type safety. In this (outdated) blog post, Jon Skeet shows a possible way to implement it . I chose to go a slightly different way (by implementing my own SynchronizationContext and using Task.Yield ). This worked fine. Then I realized there would be a problem; currently a coroutine doesn't have to finish running. It can be stopped gracefully at any point where it yields. We might have code like this: private IEnumerator Sleep(int

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

一曲冷凌霜 提交于 2019-11-28 17:23:42
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 is_generator_function(func): gen = func() gen.next() #... run the generator ... else: func() What will a straightforward implementation of is_generator_function() look like? Using the types package I can test if gen is a generator, but I wish to do so before invoking func() . Now consider the following case: def goo(): if False: yield else: return An invocation of goo() will return a

Greenlet Vs. Threads

寵の児 提交于 2019-11-28 14:56:22
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 not? Why not threads? What I am not sure about is how they can provide us with concurrency if they're basically co-routines. Matt Joiner Greenlets provide concurrency but not parallelism. Concurrency is when code can run independently of other code. Parallelism is the execution of concurrent code simultaneously. Parallelism is particularly useful when

The Pause monad

僤鯓⒐⒋嵵緔 提交于 2019-11-28 13:27:35
问题 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 then the continuation monad allows you to break people's minds! Ususally your own. ;-) But here's a challenge: Can you make a monad which can be paused ? data Pause s x instance Monad (Pause s) mutate :: (s -> s) -> Pause s () yield :: Pause s ()

Unity - IEnumerator's yield return null

坚强是说给别人听的谎言 提交于 2019-11-28 10:16:39
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" function within this while loop and why is it necessary to have it. void Start () { StartCoroutine(Move())

How to stop co-routine?

社会主义新天地 提交于 2019-11-28 08:16:22
问题 When two co-routines are running, how do you stop the first co-routine? GLOBALS.stableTime = 5; IEnumerator StableWaittingTime () { yield return new WaitForSeconds (1f); if (GLOBALS.stableTime == 0) { GameManager.instance.LevelFaildMethod (); } else { GameManager.instance.stableWaittingTime.text = GLOBALS.stableTime.ToString (); GLOBALS.stableTime--; StartCoroutine ("StableWaittingTime"); } } 回答1: @ Imapler answer is almost all you need. I would just add that StopCoroutine method of

PEP 0492 - Python 3.5 async keyword

女生的网名这么多〃 提交于 2019-11-28 04: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 async does use a reserved thread, is it a single shared thread each in their own? No, co-routines do not

How are generators and coroutines implemented in CPython?

瘦欲@ 提交于 2019-11-28 03:33:35
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 yield from that topmost frame? The yield instruction takes the current executing context as a closure,

What is the difference between a thread and a fiber?

别说谁变了你拦得住时间么 提交于 2019-11-28 02:41:08
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. 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 for your application. With threads: the current execution path may be interrupted or preempted at any time

Wait for a coroutine to finish before moving on with the function C# Unity

半世苍凉 提交于 2019-11-28 01:59:40
I was working on making a unit move through a grid in Unity2d. I got the movement to work without problems. I would want the function MovePlayer to wait until the coroutine is finished before moving on, so the program will wait until the player has finished the movement before issuing more orders. Here is my code: public class Player : MonoBehaviour { public Vector3 position; private Vector3 targetPosition; private float speed; void Awake () { speed = 2.0f; position = gameObject.transform.position; targetPosition = position; GameManager.instance.AddPlayerToList(this); //Register this player