coroutine

What are coroutines in C++20?

帅比萌擦擦* 提交于 2019-12-03 01:33:44
问题 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 回答1: At an abstract level, Coroutines split the idea of having an execution state off of the idea of having a thread of execution. SIMD (single instruction multiple data) has multiple "threads of execution" but only one execution state (it just works on multiple data). Arguably

Python gevent学习笔记 1

别来无恙 提交于 2019-12-03 01:03:54
gevent是Python的一个用于网络IO的函数库,其中应用到了 coroutine(协同程序) 的思想。首先来了解下目前网络框架的几种基本的网络I/O模型: 阻塞式单线程:这是最基本的I/O模型,只有在处理完一个请求之后才会处理下一个请求。它的缺点是效能差,如果有请求阻塞住,会让服务无法继续接受请求。但是这种模型编写代码相对简单,在应对访问量不大的情况时是非常适合的。 阻塞式多线程:针对于单线程接受请求量有限的缺点,一个很自然的想法就是给每一个请求开一个线程去处理。这样做的好处是能够接受更多的请求,缺点是在线程产生到一定数量之后,进程之间需要大量进行切换上下文的操作,会占用CPU大量的时间,不过这样处理的话编写代码的难道稍高于单进程的情况。 非阻塞式事件驱动:为了解决多线程的问题,有一种做法是利用一个循环来检查是否有网络IO的事件发生,以便决定如何来进行处理(reactor设计模式)。这样的做的好处是进一步降低了CPU的资源消耗。缺点是这样做会让程序难以编写,因为请求接受后的处理过程由reactor来决定,使得程序的执行流程难以把握。当接受到一个请求后如果涉及到阻塞的操作,这个请求的处理就会停下来去接受另一个请求,程序执行的流程不会像线性程序那样直观。twisted框架就是应用这种IO模型的典型例子。 非阻塞式Coroutine

python asyncio add_done_callback with async def

空扰寡人 提交于 2019-12-02 22:23:03
I have 2 functions: The first one, def_a , is an asynchronous function and the second one is def_b which is a regular function and called with the result of def_a as a callback with the add_done_callback function. My code looks like this: import asyncio def def_b(result): next_number = result.result() # some work on the next_number print(next_number + 1) async def def_a(number): await some_async_work(number) return number + 1 loop = asyncio.get_event_loop() task = asyncio.ensure_future(def_a(1)) task.add_done_callback(def_b) response = loop.run_until_complete(task) loop.close() And it's work

How to use code that relies on ThreadLocal with Kotlin coroutines

☆樱花仙子☆ 提交于 2019-12-02 20:36:24
Some JVM frameworks use ThreadLocal to store the call context of a application, like the SLF4j MDC , transaction managers, security managers, and others. However, Kotlin coroutines are dispatched on different threads, so how it can be made to work? (The question is inspired by GitHub issue ) Coroutine's analog to ThreadLocal is CoroutineContext . To interoperate with ThreadLocal -using libraries you need to implement a custom ContinuationInterceptor that supports framework-specific thread-locals. Here is an example. Let us assume that we use some framework that relies on a specific ThreadLocal

What's the difference between loop.create_task, asyncio.async/ensure_future and Task?

三世轮回 提交于 2019-12-02 20:13:05
I'm a little bit confused by some asyncio functions. I see there is BaseEventLoop.create_task(coro) function to schedule a co-routine. The documentation for create_task says its a new function and for compatibility we should use asyncio.async(coro) which by referring to docs again I see is an alias for asyncio.ensure_future(coro) which again schedules the execution of a co-routine. Meanwhile, I've been using Task(coro) for scheduling co-routine execution and that too seems to be working fine. so, what's the difference between all these? As you've noticed, they all do the same thing. asyncio

boost::asio::spawn yield as callback

霸气de小男生 提交于 2019-12-02 19:29:42
I'm trying to rewrite a project using boost::asio::spawn coroutines. Some parts of the project cannot be changed. For example, the storage protocol library is also written with boost::asio , but without coroutines. The problem is how to convert yield_context into a normal callback (a boost::function object or a classical functor). This is what we have in the storage library API: void async_request_data(uint64_t item_id, boost::function< void(Request_result *) > callback); As we know from examples, the asio yield context can be used like this: my_socket.async_read_some(boost::asio::buffer(data)

Coroutines in C#

牧云@^-^@ 提交于 2019-12-02 17:16:26
I am looking at ways to implement co-routines (user scheduled threads) in c#. When using c++ I was using fibers. I see on the internet fibers do not exist in C#. I would like to get similar functionality. Is there any "right" way to implement coroutines in c#? I have thought of implementing this using threads that acquire a single execution mutex + 1 on scheduler thread which releases this mutex for each coroutine. But this seems very costly (it forces a context switch between each coroutine) I have also seen the yield iterator functionality, but as I understand you can't yield within an

What are use-cases for a coroutine?

て烟熏妆下的殇ゞ 提交于 2019-12-02 17:15:52
The concept of a coroutine sounds very interesting, but I don't know, if it makes sense in a real productive environment? What are use-cases for coroutines, that can be solved more elegant, simpler or more efficient as with other methods? True coroutines require support from your tooling - they need to be implemented by the compiler and supported by the underlying framework. One real world example of Coroutines is found with the "yield return" keyword provided in C# 2.0, which allows you to write a method that returns multiple values for looping. The "yield return" does have limitations,

StartCoroutine is being called so many times (C# Unity)

两盒软妹~` 提交于 2019-12-02 17:14:53
问题 I'm creating a Pop up menu Option in Unity. Now my Problem here is that the coroutine i made in void update is being called so many times. What i mean by that is on my Unity Console the Debug.Logs are incrementing . It should not right because its already coroutine. Could some help me understand more coroutine and help me solve my little problem . Here is my code: [SerializeField] GameObject Option; [SerializeField] Button btn,btn2; [SerializeField] GameObject open, close; [SerializeField]

Is it safe to yield from within a “with” block in Python (and why)?

一世执手 提交于 2019-12-02 16:33:42
The combination of coroutines and resource acquisition seems like it could have some unintended (or unintuitive) consequences. The basic question is whether or not something like this works: def coroutine(): with open(path, 'r') as fh: for line in fh: yield line Which it does. (You can test it!) The deeper concern is that with is supposed to be something an alternative to finally , where you ensure that a resource is released at the end of the block. Coroutines can suspend and resume execution from within the with block, so how is the conflict resolved? For example, if you open a file with