coroutine

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

对着背影说爱祢 提交于 2019-11-27 17:07:21
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? 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 coroutine and it does not propagate its exception. However, a crashed child coroutine cancels its parent with

Difference between a “coroutine” and a “thread”?

房东的猫 提交于 2019-11-27 16:39:57
What are the differences between a "coroutine" and a "thread"? Alex Martelli Coroutines are a form of sequential processing: only one is executing at any given time (just like subroutines AKA procedures AKA functions -- they just pass the baton among each other more fluidly). Threads are (at least conceptually) a form of concurrent processing: multiple threads may be executing at any given time. (Traditionally, on single-CPU, single-core machines, that concurrency was simulated with some help from the OS -- nowadays, since so many machines are multi-CPU and/or multi-core, threads will de facto

python - how to implement a C-function as awaitable (coroutine)

£可爱£侵袭症+ 提交于 2019-11-27 14:58:35
问题 Environment: cooperative RTOS in C and micropython virtual machine is one of the tasks. To make the VM not block the other RTOS tasks, I insert RTOS_sleep() in vm.c:DISPATCH() so that after every bytecode is executed, the VM relinquishes control to the next RTOS task. I created a uPy interface to asynchronously obtain data from a physical data bus - could be CAN, SPI, ethernet - using producer-consumer design pattern. Usage in uPy: can_q = CANbus.queue() message = can_q.get() The

Python native coroutines and send()

我与影子孤独终老i 提交于 2019-11-27 14:43:53
问题 Generator based coroutines have a send() method which allow bidirectional communication between the caller and the callee and resumes a yielded generator coroutine from the caller. This is the functionality that turns generators into coroutines. While the new native async/await coroutines provide superior support for async I/O, I do not see how to get the equivalent of send() with them. The use of yield in async functions is explicitly forbidden, so native coroutines can return only once

Async/await as a replacement of coroutines

爷,独闯天下 提交于 2019-11-27 10:51:52
问题 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

Kotlin Coroutines the right way in Android

依然范特西╮ 提交于 2019-11-27 10:44:44
I'm trying to update a list inside the adapter using async, I can see there is too much boilerplate. Is it the right way to use Kotlin Coroutines? can this be optimized more? fun loadListOfMediaInAsync() = async(CommonPool) { try { //Long running task adapter.listOfMediaItems.addAll(resources.getAllTracks()) runOnUiThread { adapter.notifyDataSetChanged() progress.dismiss() } } catch (e: Exception) { e.printStackTrace() runOnUiThread {progress.dismiss()} } catch (o: OutOfMemoryError) { o.printStackTrace() runOnUiThread {progress.dismiss()} } } After struggling with this question for days, I

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

瘦欲@ 提交于 2019-11-27 10:08:27
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 __name__ == '__main__': loop = asyncio.get_event_loop() #futures = [asyncio.ensure_future(doit(i), loop=loop) for i in range(10)] #futures = [loop.create_task(doit(i)) for i in range(10)] futures = [doit(i) for i in range(10)] result = loop.run_until_complete(asyncio.gather(*futures)) print(result) All the three variants above that define the futures variable achieve the

How do you implement Coroutines in C++

北战南征 提交于 2019-11-27 10:07:17
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 safety seem tricky but solvable. Has it been done? Is it impossible? Ted Yes it can be done without a problem. All you need is a little assembly code to move the call stack to a newly allocated stack on the heap. I would look at the boost::coroutine library . The one thing that you should watch out for is a stack overflow. On most operating systems

Coroutine vs Continuation vs Generator

跟風遠走 提交于 2019-11-27 09:57:04
What is the difference between a coroutine and a continuation and a generator ? Keith Gaughan I'll start with generators, seeing as they're the simplest case. As @zvolkov mentioned, they're functions/objects that can be repeatedly called without returning, but when called will return (yield) a value and then suspend their execution. When they're called again, they will start up from where they last suspended execution and do their thing again. A generator is essentially a cut down (asymmetric) coroutine. The difference between a coroutine and generator is that a coroutine can accept arguments

Greenlet Vs. Threads

坚强是说给别人听的谎言 提交于 2019-11-27 08:55:55
问题 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. 回答1: Greenlets provide concurrency but not parallelism. Concurrency is when code can run independently of other code.