`

Tornado gen简介

阅读更多
[size=small]
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

简单的讲,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.

   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]
分享到:
评论

相关推荐

    tornado 4.0.1 python framework guide

    `tornado.gen` 模块提供了协程支持,使得编写异步代码变得更加简单。通过 `@gen.coroutine` 装饰器,开发者可以写出易于理解的异步代码,极大地提高了开发效率。 **3.2 tornado.ioloop — 主事件循环** `tornado....

    Tornado使用指南(中文版)

    通过定义`@tornado.gen.coroutine`装饰器的函数和使用`yield`关键字,开发者可以轻松实现异步操作。 ### 2. Web服务器和路由 Tornado的`tornado.web`模块提供了构建Web应用的基本元素,如RequestHandler、...

    tornado官方翻译文档

    2. 协程支持:Tornado通过tornado.gen模块提供了协程的支持,从而简化异步编程模型。 3. Web框架:Tornado.web模块提供了RequestHandler和Application等基础构建块,用于构建Web应用。 4. 模板系统:tornado....

    python基于Tornado实现,系统核心调度,可分布式扩展

    例如,可以使用`tornado.gen.coroutine`装饰器来定义协程,这些协程可以在等待异步操作时挂起,并在操作完成时恢复。通过这种方式,我们可以编写出复杂的异步逻辑,而无需嵌套回调,使代码更易于理解和维护。 ...

    Tornado入门的基本程序

    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,可以用于实现...

    tornado实战之一

    通过使用`asyncio`或`tornado.gen`模块,你可以编写非阻塞的回调函数,以处理I/O密集型任务,如数据库查询或网络调用。例如: ```python from tornado import gen from tornado.web import asynchronous class ...

    tornado_documentation_release3.2.2.pdf

    在异步网络编程方面,Tornado提供了tornado.gen来简化异步代码。tornado.ioloop是主事件循环,负责分派回调,而tornado.iostream提供了对非阻塞套接字的方便封装。tornado.httpclient是异步HTTP客户端,可以用来向...

    tornado 简单项目结构

    你可以使用 `tornado.gen.coroutine` 和 `yield` 关键字来编写异步处理器方法。 10. **测试**:为了确保代码质量,Tornado 项目通常会包含一个 `tests` 目录,用于存放单元测试和集成测试。可以使用 Python 的标准 ...

    tornado新版

    9. **错误处理**:Tornado提供了一种处理异常的方式,通过`@gen.coroutine`和`try/except`块,可以在异步代码中优雅地捕获和处理错误。 10. **异步HTTP客户端**:Tornado包含一个异步HTTP客户端,可以与它的服务器...

    tornado-1.2.0源码

    而`gen`模块的`@gen.coroutine`装饰器则支持协程,方便编写异步代码,提高程序执行效率。 8. **Tornado的可扩展性** Tornado的设计允许用户自定义网络协议,比如可以基于`httpserver`实现自定义的TCP服务器。此外...

    Tornado User Guide

    指南将详细介绍如何使用`@tornado.gen.coroutine`装饰器和`yield`关键字来实现协程。 5. **WebSocket支持**:Tornado对WebSocket协议提供了原生支持,使得双向通信成为可能。你将学习如何创建WebSocket服务器端和...

    Tornado 2.2 入门介绍 中文

    - **异步操作**: 利用 `tornado.gen.coroutine` 和 `yield` 关键字进行异步编程。 **4. Tornado 的典型应用场景** - **实时数据流**: 如股票报价、天气预报实时更新。 - **聊天和协作工具**: 实时消息传递,群聊...

    tornado实例todo

    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协程示例

    在Tornado中,协程主要通过`gen.coroutine`装饰器实现,允许开发者以同步的方式编写异步代码,从而简化了异步编程的复杂性。协程函数通常包含`yield`关键字,用于挂起和恢复协程的状态。 #### 二、编写协程函数 ...

    python+tornado开发的实例源码

    通过使用`@gen.coroutine`装饰器和`yield`关键字,你可以编写非阻塞的代码,等待异步操作完成而不阻塞其他请求。 7. **数据库集成**:Tornado虽然不包含内置的数据库抽象层,但它可以与各种数据库驱动程序(如...

    tornado 框架 -python web 异步

    6. **错误处理**:Tornado 提供了异常处理机制,可以通过 `@gen.coroutine` 装饰器的 `try/except` 语句来捕获和处理错误。 ### Tornado 与其他 Web 框架的比较 - 相比 Django 和 Flask 等同步框架,Tornado 在...

    Tornado项目实战

    **Tornado项目实战** Tornado是一个高性能、异步网络库,最初由FriendFeed开发,后来被Facebook收购并开源。它是Python编程语言中的一个关键组件,特别适用于构建Web服务和实时Web应用,如聊天、博客、新闻推送等。...

    4基于 Tornado 的 HTTP 服务器简介及代码组织框架(1).md

    5. 协程库:tornado.gen库,让异步编程更加直接,避免链式回调的复杂性。 Tornado的入门非常简单,通常从一个`HelloWorld`级别的应用开始。下面是一个典型的Tornado入门级代码示例: ```python import tornado....

    tornado(python)实现样例代码.

    @gen.coroutine def get(self): result = yield self.async_method() self.write(result) ``` 其中的 `async_method` 应该返回一个 `Future` 对象,`yield` 关键字会暂停当前函数的执行,直到 `Future` 完成并...

Global site tag (gtag.js) - Google Analytics