Introduction to the principle of asynchronous implementation in Python's tornado framework
This article mainly introduces the principle of the asynchronous implementation of the Tornado framework of Python, the author based on Python describes the characteristics of tornado asynchronous, the need for friends can refer to the
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.
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.
|1 2||def test (): Yield 1|
We call the test function, and instead of returning the result, we return a generator
|1 2||>>> test () <generator Object Test at 0x100b3b320>|
We call its next method to return the content after the yield keyword.
|1 2 3||>>> t = Test () >>> T.next () 1|
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.
|1 2 3||>>> 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:
|1 2||#!/usr/bin/env python #-*-Coding:utf-8-*-|
|1 2 3 4 5 6 7 8 9 10 11||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 Result "" "a = 0 ret = 1 while True:ret = yield sum, [A, ret] A, ret = ret, a print (" LOOP1 ret ", ret)|
|1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16-17||Def loop2 (): "" "" "Circular 2 is responsible for receiving functions and calculating results, and then yield out the result" "while true:func, args = yield yield func (args) print (" Loop2 ") L1 = LOOP1 () L2 = LOOP2 () tmp = L1.next () for I in Range (): 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
|1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16-17||#!/usr/bin/env python #-*-coding:utf-8-*-from __future__ import Absolute_import, print_function, Division, With_stat Ement from tornado Import Gen to Tornado Import Web from Tornado Import httpclient class Actionhandler (web. RequestHandler): @gen. Coroutine def get (self): response = yield httpclient. Asynchttpclient (). Fetch ("http://www.linuxzen.com") # ...|
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.
Tornado asynchronous can make asynchronous appear sequential and can be freed from a large string of callback.
Tornado asynchronous is not the words can be said clearly, which has a very complex encapsulation and delivery, interested in reading the source code.