coroutine

Avoid “exception ignored” in python enhanced generator

雨燕双飞 提交于 2019-12-01 03:55:14
I have a coroutine (Enhanced generators) in python with some code to be executed after the end of data: def mycoroutine(): try: while True: data = (yield) print data finally: raise ValueError print "END" co = mycoroutine() co.next() for i in (1,2,3): co.send(i) The ValueError exception is not raised but the interpreter simply prints: Exception ValueError: ValueError() in <generator object mycoroutine at 0x2b59dfa23d20> ignored Is there a way to catch the exception in the caller? The exception is raised. The finally block is executed when the generator is closed. Closing a generator is done by

Avoid “exception ignored” in python enhanced generator

孤街醉人 提交于 2019-12-01 00:50:32
问题 I have a coroutine (Enhanced generators) in python with some code to be executed after the end of data: def mycoroutine(): try: while True: data = (yield) print data finally: raise ValueError print "END" co = mycoroutine() co.next() for i in (1,2,3): co.send(i) The ValueError exception is not raised but the interpreter simply prints: Exception ValueError: ValueError() in <generator object mycoroutine at 0x2b59dfa23d20> ignored Is there a way to catch the exception in the caller? 回答1: The

C++1z Coroutines a language feature?

时光怂恿深爱的人放手 提交于 2019-11-30 20:27:21
Why will coroutines (as of now in the newest drafts for C++1z) be implemented as a core language feature (fancy keywords and all) as opposed to a library extension? There already exist a couple of implementations for them (Boost.Coroutine, etc), some of which can be made platform independent, from what i have read. Why has the committee decided to bake it into the core language itself? I'm not saying they shouldn't but Bjarne Stroustrup himself mentioned in some talk (don't know which one any more) that new features should be implemented in libraries as far as possible instead of touching the

Generator-based coroutine versus native coroutine

江枫思渺然 提交于 2019-11-30 20:10:12
I just read PEP0492 talking about the new approach on coroutines but the PEP failed to make me understand the difference between generator-based coroutines and native ones. Can someone tell me the difference (maybe with examples)? For what I understood they uses different words (yield/yield from and await/async/yield). I understand that at the end of a native coroutine a yield is expected, but this also stands true for generator-based ones. There is no functional difference. "Native coroutines" using the async and await keywords are just syntactic sugar for what was previously implemented in

Python asyncio: reader callback and coroutine communication

为君一笑 提交于 2019-11-30 18:35:13
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_event_loop() loop.add_reader(sys.stdin, handle_stdin) loop.run_until_complete(tick()) if __name__ == '

Throughput differences when using coroutines vs threading

亡梦爱人 提交于 2019-11-30 12:22:10
问题 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

How to use async/await in Python 3.5?

爷,独闯天下 提交于 2019-11-30 10:53:42
问题 #!/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() 回答1: 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

C++1z Coroutines a language feature?

北城余情 提交于 2019-11-30 04:47:58
问题 Why will coroutines (as of now in the newest drafts for C++1z) be implemented as a core language feature (fancy keywords and all) as opposed to a library extension? There already exist a couple of implementations for them (Boost.Coroutine, etc), some of which can be made platform independent, from what i have read. Why has the committee decided to bake it into the core language itself? I'm not saying they shouldn't but Bjarne Stroustrup himself mentioned in some talk (don't know which one any

Generator-based coroutine versus native coroutine

 ̄綄美尐妖づ 提交于 2019-11-30 04:13:50
问题 I just read PEP0492 talking about the new approach on coroutines but the PEP failed to make me understand the difference between generator-based coroutines and native ones. Can someone tell me the difference (maybe with examples)? For what I understood they uses different words (yield/yield from and await/async/yield). I understand that at the end of a native coroutine a yield is expected, but this also stands true for generator-based ones. 回答1: There is no functional difference. "Native

转:进程 线程 协程 管程 纤程 概念对比理解

大城市里の小女人 提交于 2019-11-30 02:49:06
引言 不知道是不是我自己本身就有那么一丝丝的密集恐惧,把这么一大堆看起来很相似很相关的概念放在一起,看起来是有点麻,捋一捋感觉舒服多了。 相关概念 任务、作业(Job,Task,Schedule) 在进程的概念出现之前,进程有着这样的称谓。 进程 为了使多个程序能够并发(同一时刻只有一个在运行,但感觉起来像多个同时运行;并行(同一时刻真的多个在运行,不是感觉像多个))的执行,操作系统需要一个结构来抽象和表示这个程序的运行。 进程是操作系统对一个正在运行的程序的一种抽象结构。 进程是指在操作系统中能独立运行并作为资源分配的基本单位,由一组机器指令、数据和堆栈等组成的能独立运行的活动实体。 操作系统可以同时运行多个进程,多个进程直接可以并发执行和交换信息。 进程在运行是需要一定的资源,如CPU、存储空间和I/O设备等。 线程 进程是资源分配的基本单位,进程的调度涉及到的内容比较多(存储空间,CPU,I/O资源等,进程现场保护),调度开销较大,在并发的切换过程效率较低。为了更高效的进行调度,提出了比进程更轻量的独立运行和调度的基本单位。 线程比进程更轻量 线程能独立运行,独立调度,拥有资源(一般是CPU资源,程序计数器等) 线程调度能大幅度减小调度的成本(相对于进程来说),线程的切换不会引起进程的切换 线程的引入进一步提高了操作系统的并发性,线程能并发执行