coroutine

Timeout for python coroutines

女生的网名这么多〃 提交于 2019-11-26 21:43:30
问题 How can I make a coroutine stop with timeout? I don't understand why asyncio.wait_for() doesn't work for me. I have such piece of code (planning to make my implementation of telnet client): def expect(self, pattern, timeout=20): if type(pattern) == str: pattern = pattern.encode('ascii', 'ignore') return self.loop.run_until_complete(asyncio.wait_for(self.asyncxpect(pattern), timeout)) async def asyncxpect(self, pattern): #receives data in a cumulative way until match is found regexp = re

How can I package a coroutine as normal function in event loop?

纵饮孤独 提交于 2019-11-26 20:00:34
问题 I am using asyncio for a network framework. In below code( low_level is our low level function, main block is our program entry, user_func is user-defined function): import asyncio loop = asyncio.get_event_loop() """:type :asyncio.AbstractEventLoop""" def low_level(): yield from asyncio.sleep(2) def user_func(): yield from low_level() if __name__ == '__main__': co = user_func() loop.run_until_complete(co) I want wrap the low_level as normal function rather than coroutine (for compatibility

How does StartCoroutine / yield return pattern really work in Unity?

谁说胖子不能爱 提交于 2019-11-26 19:15:45
I understand the principle of coroutines. I know how to get the standard StartCoroutine / yield return pattern to work in C# in Unity, e.g. invoke a method returning IEnumerator via StartCoroutine and in that method do something, do yield return new WaitForSeconds(1); to wait a second, then do something else. My question is: what's really going on behind the scenes? What does StartCoroutine really do? What IEnumerator is WaitForSeconds returning? How does StartCoroutine return control to the "something else" part of the called method? How does all this interact with Unity's concurrency model

How do stackless coroutines differ from stackful coroutines?

依然范特西╮ 提交于 2019-11-26 18:53:37
问题 Background: I'm asking this because I currently have an application with many (hundreds to thousands) of threads. Most of those threads are idle a great portion of the time, waiting on work items to be placed in a queue. When a work item comes available, it is then processed by calling some arbitrarily-complex existing code. On some operating system configurations, the application bumps up against kernel parameters governing the maximum number of user processes, so I'd like to experiment with

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

和自甴很熟 提交于 2019-11-26 18:43:22
问题 What are the differences between a "coroutine" and a "thread"? 回答1: 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

How do you implement Coroutines in C++

倖福魔咒の 提交于 2019-11-26 17:55:43
问题 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? 回答1: 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

Kotlin Coroutines the right way in Android

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 15:13: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

Unity - need to return value only after coroutine finishes

ⅰ亾dé卋堺 提交于 2019-11-26 14:46:49
I'm working on a game in unity and encountered an issue which I cannot solve. I'm connecting to a web server via standard WWW object and using a coroutine in order to execute a POST request. The code in itself works, but I need to update a variable value and return that variable once the coroutine finishes, which I'm not able to do. public int POST(string username, string passw) { WWWForm form = new WWWForm(); form.AddField("usr", username); form.AddField("pass", passw); WWW www = new WWW(url, form); StartCoroutine(WaitForRequest(www)); //problem is here ! return success_fail; } private

Equivalent C++ to Python generator pattern

萝らか妹 提交于 2019-11-26 11:48:52
问题 I\'ve got some example Python code that I need to mimic in C++. I do not require any specific solution (such as co-routine based yield solutions, although they would be acceptable answers as well), I simply need to reproduce the semantics in some manner. Python This is a basic sequence generator, clearly too large to store a materialized version. def pair_sequence(): for i in range(2**32): for j in range(2**32): yield (i, j) The goal is to maintain two instances of the sequence above, and

Available Coroutine Libraries in Java

余生颓废 提交于 2019-11-26 10:12:34
问题 I would like to do some stuff in Java that would be clearer if written using concurrent routines, but for which full-on threads are serious overkill. The answer, of course, is the use of coroutines, but there doesn\'t appear to be any coroutine support in the standard Java libraries and a quick Google on it brings up tantalising hints here or there, but nothing substantial. Here\'s what I\'ve found so far: JSIM has a coroutine class, but it looks pretty heavyweight and conflates, seemingly,