- 浏览: 247185 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (127)
- vim (3)
- python (44)
- pymysql (1)
- mysql (9)
- macvim (1)
- erlang (3)
- twisted (0)
- tornado (5)
- django (7)
- postgresql (5)
- sql (1)
- java (7)
- tech (4)
- cache (1)
- lifestyle (3)
- html (1)
- ubuntu (2)
- rabbitmq (1)
- algorithm (8)
- Linux (4)
- Pythonista (1)
- thread (1)
- sort (6)
- 设计模式 (1)
- search (1)
- Unix (6)
- Socket (3)
- C (2)
- web (1)
- gc (1)
- php (10)
- macos (1)
最新评论
-
2057:
这个程序有bug。
查找算法学习之二分查找(Python版本)——BinarySearch -
dotjar:
NB
一个Python程序员的进化[转]
[size=small]
Contains:
一、迭代器
Python supports a concept of iteration over containers. This is implemented using two distinct methods; these are used to allow user-defined classes to support iteration. Sequences, described below in more detail, always support the iteration methods.
Iteration is a process implying iterables (implementing the __iter__() method) and iterators (implementing the __next__() method). Iterables are any objects you can get an iterator from. Iterators are objects that let you iterate on iterables.
The iterator objects themselves are required to support the following two methods, which together form the iterator protocol:
1、iterator.__iter__()
Return the iterator object itself. This is required to allow both containers and iterators to be used with the for and in statements. This method corresponds to the tp_iter slot of the type structure for Python objects in the Python/C API.
2、iterator.next()
Return the next item from the container. If there are no further items, raise the StopIteration exception. This method corresponds to the tp_iternext slot of the type structure for Python objects in the Python/C API.
二、生成器
简单的讲,Generator在需要时返回中间值,能够保存当前的状态,等待下一次的返回要求。
迭代到下一次的调用时,所使用的参数都是第一次所保留下的,即是说,在整个所有函数调用的参数都是第一次所调用时保留的,而不是新创建的
Generators functions allow you to declare a function that behaves like an iterator, i.e. it can be used in a for loop.
This can be illustrated by comparing the range and xrange built-ins of Python 2.x.
Both range and xrange represent a range of numbers, and have the same function signature, but range returns a list while xrange returns a generator (at least in concept; the implementation may differ).
sum_of_numbers = sum(range(1000000))
sum_of_numbers = sum(xrange(1000000))
When we use range we build a 1,000,000 element list in memory and then find its sum. This is a waste, considering that we use these 1,000,000 elements just to compute the sum.
This waste becomes more pronounced as the number of elements (our n) becomes larger, the size of our elements become larger, or both.
On the other hand, when we use xrange, we do not incur the cost of building a 1,000,000 element list in memory. The generator created by xrange will generate each number, which sum will consume to accumulate the sum.
In the case of the "range" function, using it as an iterable is the dominant use-case, and this is reflected in Python 3.x, which makes the range built-in return a generator instead of a list.
在实现上,generator并不是生成一个列表,然后再由for对元素一个一个进行处理,而是一次只返回一个元素(用yield语句)并保存generator的状态,等下次被调用时再从当前状态往下执行,这样可以省却保存整个大列表的存储代价。
什么时候适合使用生成器:
三、yield expressions
Yield is a keyword that is used like return, except the function will return a generator.
The first time your function will run, it will run from the beginning until it hits yield, then it'll return the first value of the loop. Then, each other call will run the loop you have written in the function one more time, and return the next value, until there is no value to return.
yield简单说来就是一个生成器,生成器是这样一个函数,它记住上一次返回时咋函数体中的位置。对生成器函数的第二次(或第n次)调用跳转至该函数中间,而上次调用的所有局部变量都保持不变。
可以说带有yield的函数在python中被称之为generator.
一个带有 yield 的函数就是一个 generator,它和普通函数不同,生成一个 generator 看起来像函数调用,但不会执行任何函数代码,直到对其调用 next()(在 for 循环中会自动调用 next())才开始执行。虽然执行流程仍按函数的流程执行,但每执行到一个 yield 语句就会中断,并返回一个迭代值,下次执行时从 yield 的下一个语句继续执行。看起来就好像一个函数在正常执行的过程中被 yield 中断了数次,每次中断都会通过 yield 返回当前的迭代值。
四、tornado gen
decorator:
tornado.gen.engine(func)
Decorator for asynchronous generators.
Any generator that yields objects from this module must be wrapped in this decorator. The decorator only works on functions that are already asynchronous. For RequestHandler get/post/etc methods, this means that both the tornado.web.asynchronous and tornado.gen.engine decorators must be used (for proper exception handling, asynchronous should come before gen.engine). In most other cases, it means that it doesn’t make sense to use gen.engine on functions that don’t already take a callback argument.
Yield points:
Instances of the following classes may be used in yield expressions in the generator.
class tornado.gen.Task(func, *args, **kwargs)
Runs a single asynchronous operation.
Takes a function (and optional additional arguments) and runs it with those arguments plus a callback keyword argument. The argument passed to the callback is returned as the result of the yield expression.
A Task is equivalent to a Callback/Wait pair (with a unique key generated automatically)
class tornado.gen.Callback(key)
class tornado.gen.Wait(key)
class tornado.gen.WaitAll(keys)
示例:
参考资料:
http://wiki.python.org/moin/Generators
http://linuxgazette.net/100/pramode.html
http://www.python.org/dev/peps/pep-0234/
http://www.python.org/dev/peps/pep-0255/
http://www.pythonclub.org/python-basic/yield
http://docs.python.org/2/library/stdtypes.html
http://zouyesheng.com/generator-for-async.html
http://www.tornadoweb.org/documentation/gen.html
http://blog.xiaogaozi.org/2012/09/21/understanding-tornado-dot-gen/
http://stackoverflow.com/questions/7883962/where-to-use-yield-in-python-best
http://stackoverflow.com/questions/231767/the-python-yield-keyword-explained
[/size]
Contains:
- 1、iterator
- 2、generator
- 3、yield expressions
- 4、tornado.gen
一、迭代器
引用
Python supports a concept of iteration over containers. This is implemented using two distinct methods; these are used to allow user-defined classes to support iteration. Sequences, described below in more detail, always support the iteration methods.
Iteration is a process implying iterables (implementing the __iter__() method) and iterators (implementing the __next__() method). Iterables are any objects you can get an iterator from. Iterators are objects that let you iterate on iterables.
The iterator objects themselves are required to support the following two methods, which together form the iterator protocol:
1、iterator.__iter__()
Return the iterator object itself. This is required to allow both containers and iterators to be used with the for and in statements. This method corresponds to the tp_iter slot of the type structure for Python objects in the Python/C API.
2、iterator.next()
Return the next item from the container. If there are no further items, raise the StopIteration exception. This method corresponds to the tp_iternext slot of the type structure for Python objects in the Python/C API.
二、生成器
引用
Python’s generators provide a convenient way to implement the iterator protocol. If a container object’s __iter__() method is implemented as a generator, it will automatically return an iterator object (technically, a generator object) supplying the __iter__() and next() methods. More information about generators can be found in the documentation for the yield expression.
Generators are iterables, but you can only read them once. It's because they do not store all the values in memory
Generators are iterables, but you can only read them once. It's because they do not store all the values in memory
简单的讲,Generator在需要时返回中间值,能够保存当前的状态,等待下一次的返回要求。
迭代到下一次的调用时,所使用的参数都是第一次所保留下的,即是说,在整个所有函数调用的参数都是第一次所调用时保留的,而不是新创建的
Generators functions allow you to declare a function that behaves like an iterator, i.e. it can be used in a for loop.
引用
The performance improvement from the use of generators is the result of the lazy (on demand) generation of values, which translates to lower memory usage. Furthermore, we do not need to wait until all the elements have been generated before we start to use them. This is similar to the benefits provided by iterators, but the generator makes building iterators easy.
This can be illustrated by comparing the range and xrange built-ins of Python 2.x.
Both range and xrange represent a range of numbers, and have the same function signature, but range returns a list while xrange returns a generator (at least in concept; the implementation may differ).
sum_of_numbers = sum(range(1000000))
sum_of_numbers = sum(xrange(1000000))
When we use range we build a 1,000,000 element list in memory and then find its sum. This is a waste, considering that we use these 1,000,000 elements just to compute the sum.
This waste becomes more pronounced as the number of elements (our n) becomes larger, the size of our elements become larger, or both.
On the other hand, when we use xrange, we do not incur the cost of building a 1,000,000 element list in memory. The generator created by xrange will generate each number, which sum will consume to accumulate the sum.
In the case of the "range" function, using it as an iterable is the dominant use-case, and this is reflected in Python 3.x, which makes the range built-in return a generator instead of a list.
引用
Note: a generator will provide performance benefits only if we do not intend to use that set of generated values more than once.
在实现上,generator并不是生成一个列表,然后再由for对元素一个一个进行处理,而是一次只返回一个元素(用yield语句)并保存generator的状态,等下次被调用时再从当前状态往下执行,这样可以省却保存整个大列表的存储代价。
什么时候适合使用生成器:
- 1、You don't need to read the values twice.
- 2、You can have a lot of children and you don't want them all stored in memory.
三、yield expressions
引用
The yield expression is only used when defining a generator function, and can only be used in the body of a function definition. Using a yield expression in a function definition is sufficient to cause that definition to create a generator function instead of a normal function.
When a generator function is called, it returns an iterator known as a generator iterator, or more commonly, a generator. The body of the generator function is executed by calling the generator’s next() method repeatedly until it raises an exception.
When a yield statement is executed, the state of the generator is frozen and the value of expression_list is returned to next()‘s caller. By “frozen” we mean that all local state is retained, including the current bindings of local variables, the instruction pointer, and the internal evaluation stack: enough information is saved so that the next time next() is invoked, the function can proceed exactly as if the yield statement were just another external call.
When a generator function is called, it returns an iterator known as a generator iterator, or more commonly, a generator. The body of the generator function is executed by calling the generator’s next() method repeatedly until it raises an exception.
When a yield statement is executed, the state of the generator is frozen and the value of expression_list is returned to next()‘s caller. By “frozen” we mean that all local state is retained, including the current bindings of local variables, the instruction pointer, and the internal evaluation stack: enough information is saved so that the next time next() is invoked, the function can proceed exactly as if the yield statement were just another external call.
Yield is a keyword that is used like return, except the function will return a generator.
The first time your function will run, it will run from the beginning until it hits yield, then it'll return the first value of the loop. Then, each other call will run the loop you have written in the function one more time, and return the next value, until there is no value to return.
yield简单说来就是一个生成器,生成器是这样一个函数,它记住上一次返回时咋函数体中的位置。对生成器函数的第二次(或第n次)调用跳转至该函数中间,而上次调用的所有局部变量都保持不变。
可以说带有yield的函数在python中被称之为generator.
一个带有 yield 的函数就是一个 generator,它和普通函数不同,生成一个 generator 看起来像函数调用,但不会执行任何函数代码,直到对其调用 next()(在 for 循环中会自动调用 next())才开始执行。虽然执行流程仍按函数的流程执行,但每执行到一个 yield 语句就会中断,并返回一个迭代值,下次执行时从 yield 的下一个语句继续执行。看起来就好像一个函数在正常执行的过程中被 yield 中断了数次,每次中断都会通过 yield 返回当前的迭代值。
四、tornado gen
引用
tornado.gen is a generator-based interface to make it easier to work in an asynchronous environment. Code using the gen module is technically asynchronous, but it is written as a single generator instead of a collection of separate functions.
decorator:
tornado.gen.engine(func)
Decorator for asynchronous generators.
Any generator that yields objects from this module must be wrapped in this decorator. The decorator only works on functions that are already asynchronous. For RequestHandler get/post/etc methods, this means that both the tornado.web.asynchronous and tornado.gen.engine decorators must be used (for proper exception handling, asynchronous should come before gen.engine). In most other cases, it means that it doesn’t make sense to use gen.engine on functions that don’t already take a callback argument.
Yield points:
Instances of the following classes may be used in yield expressions in the generator.
class tornado.gen.Task(func, *args, **kwargs)
Runs a single asynchronous operation.
Takes a function (and optional additional arguments) and runs it with those arguments plus a callback keyword argument. The argument passed to the callback is returned as the result of the yield expression.
A Task is equivalent to a Callback/Wait pair (with a unique key generated automatically)
class tornado.gen.Callback(key)
class tornado.gen.Wait(key)
class tornado.gen.WaitAll(keys)
示例:
class GenAsyncHandler(RequestHandler): @asynchronous @gen.engine def get(self): http_client = AsyncHTTPClient() response = yield gen.Task(http_client.fetch, "http://example.com") do_something_with_response(response) self.render("template.html")
参考资料:
http://wiki.python.org/moin/Generators
http://linuxgazette.net/100/pramode.html
http://www.python.org/dev/peps/pep-0234/
http://www.python.org/dev/peps/pep-0255/
http://www.pythonclub.org/python-basic/yield
http://docs.python.org/2/library/stdtypes.html
http://zouyesheng.com/generator-for-async.html
http://www.tornadoweb.org/documentation/gen.html
http://blog.xiaogaozi.org/2012/09/21/understanding-tornado-dot-gen/
http://stackoverflow.com/questions/7883962/where-to-use-yield-in-python-best
http://stackoverflow.com/questions/231767/the-python-yield-keyword-explained
[/size]
发表评论
-
macos 10.9.2 clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command
2014-03-25 19:13 1760方法总是有的,当然需要你去寻找。 当然如果花费太多的时间在一件 ... -
PostgreSQL psycopg2:IndexError: tuple index out of range
2014-01-09 17:04 2231Postgresql psycopg2使用like查询的时候 ... -
Python 迭代器和生成器
2013-10-15 23:09 2850迭代器 迭代器只不过是一个实现迭代器协议的容器对象。它基于两个 ... -
Python时间模块
2013-10-15 23:03 3469time模块 时间模块中最常用的一个函数就是获取当前时间的函数 ... -
Python装饰器
2013-10-15 22:59 1569编写自定义装饰器有许多方法,但最简单和最容易理解的方法是编写一 ... -
python list
2013-10-15 22:56 1254简单总结以及整理如下: >>> dir( ... -
Python Excel
2013-09-10 17:21 976安装lib easy_install xlrd def ... -
排序算法学习(python版本)之堆排序(HeapSort)
2013-07-01 22:54 1997Contains: 堆排序以及堆排序的应用 堆排序(Heaps ... -
python range xrange
2013-06-25 23:30 1149引用Help on built-in function ran ... -
python class
2013-06-25 00:54 1829引用类是创建新对象类 ... -
AttributeError: 'module' object has no attribute 'SendCloud'
2013-06-05 11:46 7083网上查了下 意思是说你命名的文件名不能和lib重名,这样会导 ... -
python string
2013-05-07 23:44 2198如果这就是字符串,这本来就是字符串 首先看下字符串的方法 ... -
Python property
2013-03-29 19:56 0由于之前有总结过,可以参考http://2057.iteye. ... -
python tips
2013-03-28 23:57 8831、enum #!/usr/bin/env python ... -
python decorators
2013-03-28 23:36 1365Contains: 1、decorators 2、funct ... -
python closures
2013-03-28 22:09 1190Closure:如果在一个内部函数里,对在外部作用域(但不是在 ... -
Python map、filter,reduce介绍
2013-03-28 22:02 13091、filter(function,iterable) 引用C ... -
Python __new__ 、__init__、 __call__
2013-03-26 23:49 5351Contains: __new__: 创建对象时调用,返回当 ... -
Python socket简介
2013-03-25 23:42 2168自豪地使用dir和help. Python 2.7.2 ( ... -
Tornado ioloop源码简析
2013-03-21 00:18 2849#!/usr/bin/env python #-*-en ...
相关推荐
`tornado.gen` 模块提供了协程支持,使得编写异步代码变得更加简单。通过 `@gen.coroutine` 装饰器,开发者可以写出易于理解的异步代码,极大地提高了开发效率。 **3.2 tornado.ioloop — 主事件循环** `tornado....
通过定义`@tornado.gen.coroutine`装饰器的函数和使用`yield`关键字,开发者可以轻松实现异步操作。 ### 2. Web服务器和路由 Tornado的`tornado.web`模块提供了构建Web应用的基本元素,如RequestHandler、...
2. 协程支持:Tornado通过tornado.gen模块提供了协程的支持,从而简化异步编程模型。 3. Web框架:Tornado.web模块提供了RequestHandler和Application等基础构建块,用于构建Web应用。 4. 模板系统:tornado....
例如,可以使用`tornado.gen.coroutine`装饰器来定义协程,这些协程可以在等待异步操作时挂起,并在操作完成时恢复。通过这种方式,我们可以编写出复杂的异步逻辑,而无需嵌套回调,使代码更易于理解和维护。 ...
from tornado import gen @gen.coroutine def async_handler(self): result = yield self.fetch("http://example.com") self.write(result.body) ``` 4. WebSocket:Tornado也支持WebSocket,可以用于实现...
通过使用`asyncio`或`tornado.gen`模块,你可以编写非阻塞的回调函数,以处理I/O密集型任务,如数据库查询或网络调用。例如: ```python from tornado import gen from tornado.web import asynchronous class ...
在异步网络编程方面,Tornado提供了tornado.gen来简化异步代码。tornado.ioloop是主事件循环,负责分派回调,而tornado.iostream提供了对非阻塞套接字的方便封装。tornado.httpclient是异步HTTP客户端,可以用来向...
你可以使用 `tornado.gen.coroutine` 和 `yield` 关键字来编写异步处理器方法。 10. **测试**:为了确保代码质量,Tornado 项目通常会包含一个 `tests` 目录,用于存放单元测试和集成测试。可以使用 Python 的标准 ...
9. **错误处理**:Tornado提供了一种处理异常的方式,通过`@gen.coroutine`和`try/except`块,可以在异步代码中优雅地捕获和处理错误。 10. **异步HTTP客户端**:Tornado包含一个异步HTTP客户端,可以与它的服务器...
而`gen`模块的`@gen.coroutine`装饰器则支持协程,方便编写异步代码,提高程序执行效率。 8. **Tornado的可扩展性** Tornado的设计允许用户自定义网络协议,比如可以基于`httpserver`实现自定义的TCP服务器。此外...
指南将详细介绍如何使用`@tornado.gen.coroutine`装饰器和`yield`关键字来实现协程。 5. **WebSocket支持**:Tornado对WebSocket协议提供了原生支持,使得双向通信成为可能。你将学习如何创建WebSocket服务器端和...
- **异步操作**: 利用 `tornado.gen.coroutine` 和 `yield` 关键字进行异步编程。 **4. Tornado 的典型应用场景** - **实时数据流**: 如股票报价、天气预报实时更新。 - **聊天和协作工具**: 实时消息传递,群聊...
connection = yield tornado.gen.Task(self.db.connect) cursor = yield connection.cursor() yield cursor.execute("INSERT INTO todos (content) VALUES (%s)", (todo,)) yield connection.commit() cursor....
在Tornado中,协程主要通过`gen.coroutine`装饰器实现,允许开发者以同步的方式编写异步代码,从而简化了异步编程的复杂性。协程函数通常包含`yield`关键字,用于挂起和恢复协程的状态。 #### 二、编写协程函数 ...
通过使用`@gen.coroutine`装饰器和`yield`关键字,你可以编写非阻塞的代码,等待异步操作完成而不阻塞其他请求。 7. **数据库集成**:Tornado虽然不包含内置的数据库抽象层,但它可以与各种数据库驱动程序(如...
6. **错误处理**:Tornado 提供了异常处理机制,可以通过 `@gen.coroutine` 装饰器的 `try/except` 语句来捕获和处理错误。 ### Tornado 与其他 Web 框架的比较 - 相比 Django 和 Flask 等同步框架,Tornado 在...
**Tornado项目实战** Tornado是一个高性能、异步网络库,最初由FriendFeed开发,后来被Facebook收购并开源。它是Python编程语言中的一个关键组件,特别适用于构建Web服务和实时Web应用,如聊天、博客、新闻推送等。...
5. 协程库:tornado.gen库,让异步编程更加直接,避免链式回调的复杂性。 Tornado的入门非常简单,通常从一个`HelloWorld`级别的应用开始。下面是一个典型的Tornado入门级代码示例: ```python import tornado....
@gen.coroutine def get(self): result = yield self.async_method() self.write(result) ``` 其中的 `async_method` 应该返回一个 `Future` 对象,`yield` 关键字会暂停当前函数的执行,直到 `Future` 完成并...