coroutine

Why doesn't asyncio always use executors?

最后都变了- 提交于 2019-11-28 01:52:19
问题 I have to send a lot of HTTP requests, once all of them have returned, the program can continue. Sounds like a perfect match for asyncio . A bit naively, I wrapped my calls to requests in an async function and gave them to asyncio . This doesn't work. After searching online, I found two solutions: use a library like aiohttp, which is made to work with asyncio wrap the blocking code in a call to run_in_executor To understand this better, I wrote a small benchmark. The server-side is a flask

Timeout for python coroutines

自闭症网瘾萝莉.ら 提交于 2019-11-28 00:31:50
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.compile(b'(?P<payload>[\s\S]*)(?P<pattern>%s)' %pattern) self.buffer = b'' while True: # add timeout # add

Implement yield and send in Scheme

扶醉桌前 提交于 2019-11-27 23:19:47
I'm trying to port yield and yield from from Python to Scheme. Here is an implementation I've done: (define (coroutine routine) (let ((current routine) (status 'new)) (lambda* (#:optional value) (let ((continuation-and-value (call/cc (lambda (return) (let ((returner (lambda (value) (call/cc (lambda (next) (return (cons next value))))))) (if (equal? status 'new) (begin (set! status 'running) (current returner)) (current (cons value returner))) (set! status 'dead)))))) (if (pair? continuation-and-value) (begin (set! current (car continuation-and-value)) (cdr continuation-and-value)) continuation

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

强颜欢笑 提交于 2019-11-27 20:16:46
问题 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. 回答1: Short answer: yield from is an

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

别等时光非礼了梦想. 提交于 2019-11-27 20:14:39
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 etc.), but low_level is in event loop. How can wrap it as a normal function? Because low_level is a

In python is there a way to check if a function is a “generator function” before calling it?

风格不统一 提交于 2019-11-27 20:10:14
问题 Lets say I have two functions: def foo(): return 'foo' def bar(): yield 'bar' The first one is a normal function, and the second is a generator function. Now I want to write something like this: def run(func): if is_generator_function(func): gen = func() gen.next() #... run the generator ... else: func() What will a straightforward implementation of is_generator_function() look like? Using the types package I can test if gen is a generator, but I wish to do so before invoking func() . Now

Can “experimental” Kotlin coroutines be used in production?

无人久伴 提交于 2019-11-27 18:39:17
Can Kotlin coroutines be used in production, and what does their experimental status mean? UPDATE : Kotlin coroutines are no longer experimental as of Kotlin 1.3. Kotlin coroutines can and should be used in production. That was the chief reason to officially release them in Kotlin 1.1. Having released them, the JetBrains team had committed to maintain backwards compatibility with respect to any changes that are introduced to them in the minor releases as they evolve, while allowing people to safely try them in complex production applications. In short, the difference between “experimental” and

Is coroutine a new thread in Unity3D?

青春壹個敷衍的年華 提交于 2019-11-27 17:18:04
问题 I am confused and curious about how coroutines (in Unity3D and perhaps other places) work. Is coroutine a new thread? Unity's documentation they said: A coroutine is a function that can suspend its execution (yield) until the given YieldInstruction finishes. And they have C# examples here: using UnityEngine; using System.Collections; public class example : MonoBehaviour { void Start() { print("Starting " + Time.time); StartCoroutine(WaitAndPrint(2.0F)); print("Before WaitAndPrint Finishes " +

How do stackless coroutines differ from stackful coroutines?

不打扰是莪最后的温柔 提交于 2019-11-27 17:12:33
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 means to reduce the number of worker threads. My proposed solution: It seems like a coroutine-based

Test if function or method is normal or asynchronous

为君一笑 提交于 2019-11-27 17:09:28
问题 How can I find out if a function or method is a normal function or an async function? I would like my code to automatically support normal or async callbacks and need a way to test what type of function is passed. async def exampleAsyncCb(): pass def exampleNomralCb(): pass def isAsync(someFunc): #do cool dynamic python stuff on the function return True/False async def callCallback(cb, arg): if isAsync(cb): await cb(arg) else: cb(arg) And depending on what type of function gets passed it