coroutine

Throughput differences when using coroutines vs threading

▼魔方 西西 提交于 2019-11-30 02:30:56
A few days ago I has asked a question on SO about helping me design a paradigm for structuring multiple HTTP requests Here's the scenario. I would like a have a multi-producer, multi-consumer system. My producers crawl and scrape a few sites and add the links that it finds into a queue. Since I'll be crawling multiple sites, I would like to have multiple producers/crawlers. The consumers/workers feed off this queue, make TCP/UDP requests to these links and saves the results to my Django DB. I would also like to have multiple-workers as each queue item is totally independent of each other.

Fibers in C#: are they faster than iterators, and have people used them?

允我心安 提交于 2019-11-30 02:27:39
So I was chatting with a colleague about fibers and turned up this paper from 2003 that describes a implementation of coroutines in C# using the Fiber API. The implementation of Yield in this paper was for .NET 1.1, so it predates the yield return syntax that appeared in .NET 2.0. It definitely looks, at first glance, that the implementation here is potentially faster and could scale across multiple CPUs rather well. Has anyone used it? I haven't used it, but I have an interest in the subject. Here's one nice implementation of coroutines in C# with a round-robin scheduler: http://www

秒杀 tj/co 的 hprose 协程库

妖精的绣舞 提交于 2019-11-30 01:13:07
tj/co 有以下几个方面的问题: 首先, tj/co 库中的 yield 只支持 thunk 函数,生成器函数,promise 对象,以及数组和对象,但是不支持普通的基本类型的数据,比如 null , 数字,字符串等都不支持。这对于 yield 一个类型不确定的变量来说,是很不方便的。而且这跟 await 也是不兼容的。 其次,在 yield 数组和对象时, tj/co 库会自动对数组中的元素和对象中的字段递归的遍历,将其中的所有的 Promise 元素和字段替换为实际值,这对于简单的数据来说,会方便一些。但是对于带有循环引用的数组和对象来说,会导致无法获取到结果,这是一个致命的问题。即使对于不带有循环引用结构的数组和对象来说,如果该数组和对象比较复杂,这也会消耗大量的时间。而且这跟 await 也是不兼容的。 再次,对于 thunk 函数, tj/co 库会认为回调函数第一个参数必须是表示错误,从第二个参数开始才表示返回值。而这对于回调函数只有一个返回值参数的函数,或者回调函数的第一个参数不表示错误的函数来说, tj/co 库就无法使用了。 而 hprose.co 对 yield 的支持则跟 await 完全兼容,支持对所有类型的数据进行 yield 。 当 hprose.co 对 chunk 函数进行 yield 时,如果回调函数第一个参数是 Error

How to create an event loop with rolling coroutines running on it forever?

折月煮酒 提交于 2019-11-30 00:18:24
In order to prevent from context switching, I want to create a big loop to serve both the network connections and some routines. Here's the implementation for normal functions: import asyncio import time def hello_world(loop): print('Hello World') loop.call_later(1, hello_world, loop) def good_evening(loop): print('Good Evening') loop.call_later(1, good_evening, loop) print('step: asyncio.get_event_loop()') loop = asyncio.get_event_loop() print('step: loop.call_soon(hello_world, loop)') loop.call_soon(hello_world, loop) print('step: loop.call_soon(good_evening, loop)') loop.call_soon(good

How to create an event loop with rolling coroutines running on it forever?

别来无恙 提交于 2019-11-30 00:16:28
In order to prevent from context switching, I want to create a big loop to serve both the network connections and some routines. Here's the implementation for normal functions: import asyncio import time def hello_world(loop): print('Hello World') loop.call_later(1, hello_world, loop) def good_evening(loop): print('Good Evening') loop.call_later(1, good_evening, loop) print('step: asyncio.get_event_loop()') loop = asyncio.get_event_loop() print('step: loop.call_soon(hello_world, loop)') loop.call_soon(hello_world, loop) print('step: loop.call_soon(good_evening, loop)') loop.call_soon(good

What does the “yield from” syntax do in asyncio and how is it different from “await”

蹲街弑〆低调 提交于 2019-11-29 23:54:09
From the perspective of someone who has written asyncio code but is looking to better understand the inner workings, what is yield from , await and how are those useful for allowing asynchronous code? There is one highly upvoted question asking about the uses of the yield from syntax and one explaining async and await , but both go in depth about different topics and are not really a concise explanation of the underlying code and how it fits in with asyncio. Mikhail Gerasimov Short answer: yield from is an old way to wait for asyncio's coroutine. await is an modern way to wait for asyncio's

How to use async/await in Python 3.5?

这一生的挚爱 提交于 2019-11-29 22:48:29
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import time async def foo(): await time.sleep(1) foo() I couldn't make this dead simple example to run: RuntimeWarning: coroutine 'foo' was never awaited foo() Running coroutines requires an event loop . Use the asyncio() library to create one: import asyncio # Python 3.7+ asyncio.run(foo()) or # Python 3.6 and older loop = asyncio.get_event_loop() loop.run_until_complete(foo()) Also see the Tasks and Coroutines chapter of the asyncio documentation . If you already have a loop running, you'd want to run additional coroutines concurrently by

What are Lua coroutines even for? Why doesn't this code work as I expect it?

断了今生、忘了曾经 提交于 2019-11-29 22:14:43
I'm having trouble understanding this code... I was expecting something similar to threading where I would get an output with random "nooo" and "yaaaay"s interspersed with each other as they both do the printing asynchronously, but rather I discovered that the main thread seems to block on the first calling of coroutine.resume() and thus prevents the next from being started until the first has yielded. If this is the intended operation coroutines, what are they useful for, and how would I achieve the goal I was hoping for? Would I have to implement my own scheduler for these coroutines to

The Pause monad

蓝咒 提交于 2019-11-29 18:43:01
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 () step :: s -> Pause s () -> (s, Maybe (Pause s ())) The Pause monad is a kind of state monad (hence mutate

How to stop co-routine?

时光怂恿深爱的人放手 提交于 2019-11-29 14:38:48
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"); } } @ Imapler answer is almost all you need. I would just add that StopCoroutine method of MonoBehaviour is overloaded and has 3 types of parameters, so it is possible to stop many coroutines of same name.