coroutine

Safe cross platform coroutines

一笑奈何 提交于 2019-12-03 12:41:00
All coroutine implementations I've encountered use assembly or inspect the contents of jmp_buf . The problem with this is it inherently not cross platform. I think the following implementation doesn't go off into undefined behavior or rely on implementation details. But I've never encountered a coroutine written like this. Is there some inherent flaw is using long jump with threads? Is there some hidden gotcha in this code? #include <setjmp.h> #include <thread> class Coroutine { public: Coroutine( void ) : m_done( false ), m_thread( [&](){ this->start(); } ) { } ~Coroutine( void ) { std::lock

Will a Python generator be garbage collected if it will not be used any more but hasn't reached StopIteration yet?

走远了吗. 提交于 2019-12-03 10:56:46
When a generator is not used any more, it should be garbage collected, right? I tried the following code but I am not sure which part I was wrong. import weakref import gc def countdown(n): while n: yield n n-=1 cd = countdown(10) cdw = weakref.ref(cd)() print cd.next() gc.collect() print cd.next() gc.collect() print cdw.next() On the second last line, I called garbage collector and since there is no call to cd any more. gc should free cd right. But when I call cdw.next() , it is still printing 8. I tried a few more cdw.next() , it could successfully print all the rest until StopIteration. I

Making Django go green

馋奶兔 提交于 2019-12-03 10:27:19
问题 I have a Django management command that makes thousands of TCP/UDP requests. I've used Gevent to speed this up as I've restructured my code to work as coroutines. The socket connections no longer block but from what I've read, parts of Django still aren't green. (By green, I mean using greenlets.) Could you tell me what parts of Django aren't green and what I can do to make them green? There are some DB related parts that still block I think. Are there any libraries/patches for Django that

why my coroutine blocks whole tornado instance?

百般思念 提交于 2019-12-03 09:02:07
from tornado import web, gen import tornado, time class CoroutineFactorialHandler(web.RequestHandler): @web.asynchronous @gen.coroutine def get(self, n, *args, **kwargs): n = int(n) def callbacker(iterator, callback): try: value = next(iterator) except StopIteration: value = StopIteration callback(value) def factorial(n): x = 1 for i in range(1, n+1): x *= i yield yield x iterator = factorial(n) t = time.time() self.set_header("Content-Type", "text/plain") while True: response = yield gen.Task(callbacker, iterator) #log.debug("response: %r" %response) if response is StopIteration: break elif

python asyncio add_done_callback with async def

旧时模样 提交于 2019-12-03 07:26:38
问题 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

Wait for the termination of n goroutines

半城伤御伤魂 提交于 2019-12-03 07:19:31
I need to start a huge amount of goroutines and wait for their termination. The intuitive way seems to use a channel to wait till all of them are finished : package main type Object struct { //data } func (obj *Object) Update(channel chan int) { //update data channel <- 1 return } func main() { channel := make(chan int, n) list := make([]Object, n, m) for { for _, object := range list { go object.Update(channel) } for i := 0; i < n; i++ { <-channel } //now everything has been updated. start again } } But the problem is that the amount of objects and therefore the amount of goroutines could

How to use code that relies on ThreadLocal with Kotlin coroutines

北城以北 提交于 2019-12-03 06:57:25
问题 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) 回答1: 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

boost::asio::spawn yield as callback

夙愿已清 提交于 2019-12-03 06:01:44
问题 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

Curl 在 Swoole 协程中的解决方案

邮差的信 提交于 2019-12-03 03:08:39
众所周知,在 Swoole 应用中,是不推荐使用 Curl 的,因为 Curl 会阻塞进程。 本文会用实际的代码和数据,用最直观的方式,让你明白为什么。 最后还会给出 Curl 在 Swoole 中的解决方案,如果不想看分析可以直接拉到最后。 例程对比 宇润看文章不喜欢那些虚的,所以自己写也比较实在,直接来跑一下代码,用数据看为什么不推荐在 Swoole 使用 Curl。 为了偷懒,我直接用了 YurunHttp 的 Curl 和 Swoole Handler,来替代那些又臭又长的 Curl 代码。 代码 composer.json { "require": { "yurunsoft/yurun-http": "~3.0" } } server.php <?php $http = new Swoole\Http\Server('127.0.0.1', 9501); $http->on('workerstart', function(){ \Swoole\Runtime::enableCoroutine(); }); $http->on('request', function ($request, $response) { sleep(1); // 假设各种处理耗时1秒 $response->end($request->get['id'] . ': ' . date('Y-m-d H

Coroutines in C#

独自空忆成欢 提交于 2019-12-03 02:22:10
问题 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)