coroutine

Python asyncio: reader callback and coroutine communication

依然范特西╮ 提交于 2019-12-18 20:54:12
问题 I am trying to implement a simple idea of passing a data from stdin to a coroutine: import asyncio import sys event = asyncio.Event() def handle_stdin(): data = sys.stdin.readline() event.data = data # NOTE: data assigned to the event object event.set() @asyncio.coroutine def tick(): while 1: print('Tick') yield from asyncio.sleep(1) if event.is_set(): data = event.data # NOTE: data read from the event object print('Data received: {}'.format(data)) event.clear() def main(): loop = asyncio.get

Calling coroutines in asyncio.Protocol.data_received

三世轮回 提交于 2019-12-18 13:13:14
问题 I am having a problem doing asynchronous stuff in the asyncio.Protocol.data_received callback of the new Python asyncio module. Consider the following server: class MathServer(asyncio.Protocol): @asyncio.coroutine def slow_sqrt(self, x): yield from asyncio.sleep(1) return math.sqrt(x) def fast_sqrt(self, x): return math.sqrt(x) def connection_made(self, transport): self.transport = transport #@asyncio.coroutine def data_received(self, data): print('data received: {}'.format(data.decode())) x

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

我怕爱的太早我们不能终老 提交于 2019-12-18 10:53:16
问题 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

Synchronised threads coroutines

你离开我真会死。 提交于 2019-12-18 07:06:24
问题 I'm trying to get threads to wait for each other before preceding so they stay in sync. In my actual program I have lots of IObjectObserved objects (on their own threads) sending out events and I want to keep everything in sync so an IObjectListener (on its own thread) can listen to one of these objects 50 times and then subscribe to another in time to catch its 51st event. I haven't got that far yet, but I think synchronizing threads is the main problem. I'm managed to achieve this with two

Implement yield and send in Scheme

痞子三分冷 提交于 2019-12-17 16:23:12
问题 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

asyncio.ensure_future vs. BaseEventLoop.create_task vs. simple coroutine?

浪尽此生 提交于 2019-12-17 10:14:28
问题 I've seen several basic Python 3.5 tutorials on asyncio doing the same operation in various flavours. In this code: import asyncio async def doit(i): print("Start %d" % i) await asyncio.sleep(3) print("End %d" % i) return i if __name__ == '__main__': loop = asyncio.get_event_loop() #futures = [asyncio.ensure_future(doit(i), loop=loop) for i in range(10)] #futures = [loop.create_task(doit(i)) for i in range(10)] futures = [doit(i) for i in range(10)] result = loop.run_until_complete(asyncio

Coroutine vs Continuation vs Generator

三世轮回 提交于 2019-12-17 10:09:41
问题 What is the difference between a coroutine and a continuation and a generator ? 回答1: I'll start with generators, seeing as they're the simplest case. As @zvolkov mentioned, they're functions/objects that can be repeatedly called without returning, but when called will return (yield) a value and then suspend their execution. When they're called again, they will start up from where they last suspended execution and do their thing again. A generator is essentially a cut down (asymmetric)

Turn functions with a callback into Python generators?

空扰寡人 提交于 2019-12-17 07:24:01
问题 The Scipy minimization function (just to use as an example), has the option of adding a callback function at each step. So I can do something like, def my_callback(x): print x scipy.optimize.fmin(func, x0, callback=my_callback) Is there a way to use the callback function to create a generator version of fmin, so that I could do, for x in my_fmin(func,x0): print x It seems like it might be possible with some combination of yields and sends, but I can quite think of anything. 回答1: As pointed in

lua5.4初体验

空扰寡人 提交于 2019-12-16 20:11:22
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> -->const 变量的定义,table 中不能有 const 值,但可以定义一个常量table local a <const> = 4444 local test_t <const> = { a = undef, b = 100, } print(string.format('%p', a)) print(a) print() -->%p是新增的格式化,如果是table、函數、coroutine,会输出地址,变量则会输出`(nil)` print(string.format('%p', {})) print(string.format('%p', print)) print(string.format('%p', 2)) print(string.format('%p', const)) print(string.format("%p", coroutine.running())) -- test_t = {} --> 这样会报错 -->想要返回一个常量,目前只能这么写 local function get_a_number() local b <const> = 5555 return b end local c <const> = get_a_number() 来源: oschina 链接: https:

python协程 生成器

笑着哭i 提交于 2019-12-15 18:31:10
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 协程,又称微线程,纤程。英文名Coroutine。 线程是系统级别的它们由操作系统调度,而协程则是程序级别的由程序根据需要自己调度。在一个线程中会有很多函数,我们把这些函数称为子程序,在子程序执行过程中可以中断去执行别的子程序,而别的子程序也可以中断回来继续执行之前的子程序,这个过程就称为协程。也就是说在同一线程内一段代码在执行过程中会中断然后跳转执行别的代码,接着在之前中断的地方继续开始执行,类似与yield操作。 协程拥有自己的寄存器上下文和栈。协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来的时候,恢复先前保存的寄存器上下文和栈。因此:协程能保留上一次调用时的状态(即所有局部状态的一个特定组合),每次过程重入时,就相当于进入上一次调用的状态,换种说法:进入上一次离开时所处逻辑流的位置。 因为协程是一个线程执行,那怎么利用多核CPU呢?最简单的方法是多进程+协程,既充分利用多核,又充分发挥协程的高效率,可获得极高的性能。 协程的优点: 无需线程上下文切换的开销,协程避免了无意义的调度,由此可以提高性能(但也因此,程序员必须自己承担调度的责任,同时,协程也失去了标准线程使用多CPU的能力) 无需原子操作锁定及同步的开销 方便切换控制流,简化编程模型 高并发+高扩展性+低成本