Tornado coroutine asynchronization based on Python generator

Source: Internet
Author: User
Tags generator

Tornado 4.0 has been released for a long time, and the new version has widely applied the Future feature. We have now upgraded the Tornado to the latest version, and have also used a lot of the coprocessor features.

A long time no update blog, today is a simple introduction to the implementation of the Tornado, the Tornado is based on the Python generator implementation, so first of all to review the generator.

Build device

The Python generator can save the execution state and restore it at the next call, by using the yield keyword in the function body to create a generator, restoring the generator's state through the built-in function next or the next method of the generator.

The code is as follows Copy Code

def test ():
Yield 1

We call the test function, and instead of returning the result, we return a generator

The code is as follows Copy Code
>>> Test ()
<generator Object Test at 0x100b3b320>

We call its next method to return the content after the yield keyword.

The code is as follows Copy Code

>>> t = Test ()
>>> T.next ()

If we then call the next method and the yield keyword continues to return, a Stopiteration exception is thrown.

The yield keyword is not only able to return the state from within the generator, but it can also pass the external information to the generator, by assigning the Yeild key to the variable, and by calling the generator's send method to pass the object inside the generator. It is important to note that the start of the generator must call its next method, and the next action will also be triggered by the subsequent send method call. If no variable receives the yield keyword, the value sent by send will be discarded.

The code is as follows Copy Code

>>> def Test ():
A = yield
Print (a)

The generator returned by the above function is called first to return none, and if you call next directly it will send none to the generator, and if call send sends a value, it will print the value and throw the stopiteration exception.

A simple co-process
The above is all the foundation of the implementation of the process, in order to deepen understanding, we write a small example, the example we only use the coprocessor to open two or more dead loops, the following is an extremely simple example:

The code is as follows Copy Code

#!/usr/bin/env python
#-*-Coding:utf-8-*-

From __future__ import Absolute_import, print_function, Division, with_statement


Def LOOP1 ():
"" "Loop 1 is responsible for throwing a function and corresponding arguments, and receiving the result
"""
A = 0
RET = 1
While True:
RET = yield sum, [A, ret]
A, ret = RET, a
Print ("LOOP1 ret", ret)


Def LOOP2 ():
"" "Loop 2 is responsible for receiving the function and calculating the result, and then yield the result
"""
While True:
Func, args = yield
Yield func (args)
Print ("Loop2")


L1 = Loop1 ()
L2 = Loop2 ()
TMP = L1.next ()

For I in range (10):
L2.next ()
RET = l2.send (TMP)
TMP = L1.send (ret)

In the example above LOOP1 is responsible for generating tasks, LOOP2 is responsible for performing tasks, the main loop is responsible for scheduling tasks and sending the results back to the task creator.

Tornado how to do it.

Let's first look at an example of using Tornado asynchronous

  code is as follows copy code

#!/usr/bin /env python
#-*-coding:utf-8-*-

From __future__ import Absolute_import, print_function, Division, With_state ment

From tornado import Gen
to Tornado Import Web
from Tornado import httpclient


Class Actio Nhandler (web. RequestHandler):

    @gen. Coroutine
    def get (self):
         response = yield httpclient. Asynchttpclient (). Fetch ("http://www.111cn.net")

        # ...

In fact, the principle in the above simple example has been made clear, let us briefly analyze the above example, first Tornado get the Actionhandler.get method throw (next) a task, and then asynchronously to perform the task, when the Task (network request) end or abnormal Tornado gets the event notification and then puts the result back (send) into the method so that the method continues to execute.

Because it is asynchronous, calling this method does not block other tasks from executing.

At this point our approach is actually the last example of the LOOP1 function, while the Tornado dispatch and executes the task it throws.

Summarize

Tornado asynchronous can make asynchronous appear sequential and can be freed from a large string of callback.

Tornado asynchronous is not this words can be said clearly, which has a very complex packaging and delivery, interested in reading the source code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.