This section briefly introduces the coroutine asynchronous implementation principle in Python's Tornado framework, and pythontornado

Source: Internet
Author: User

This section briefly introduces the coroutine asynchronous implementation principle in Python's Tornado framework, and pythontornado

Tornado 4.0 has been released for a long time. The new version is widely used in the Future feature. We have upgraded Tornado to the latest version, and we have also used the coroutine feature extensively.

I haven't updated my blog for a long time. Today I will briefly introduce the implementation principle of Tornado coroutine. The coroutine of Tornado is implemented based on the Python generator. So I will first review the generator.
Generator

The Python generator can save the execution status and restore it during the next call. You can create a generator by using the yield keyword in the function body, use the built-in function next or the next method of the generator to restore the generator state.

def test():  yield 1

When we call the test function, a generator is returned instead of the result.

>>> test()<generator object test at 0x100b3b320>

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

>>> t = test()>>> t.next()1

If we continue to call the next method without the yield keyword, A StopIteration exception will be thrown.

The yield keyword not only returns the status from inside the generator, but also transmits the external information to the generator. by assigning the yeild key to the variable, and call the send method of the generator to pass the object to the generator. note that the next method must be called at the beginning of the generator, and the next action will be triggered when the send method is called later. if no variable receives the yield keyword, the value passed by send will be discarded.

>>> def test():  a = yield  print(a)

First, the generator returned by calling the next function will return None. If you call next directly at this time, it will send None to the generator. If you call send to send a value, this value will be printed and a StopIteration exception will be thrown.
A simple coroutine

The above is all the basis for implementing coroutine. To deepen our understanding, we will write a small example here. In this example, we only use coroutine to open two or more infinite loops. Below is an extremely simple example::

#!/usr/bin/env python# -*- coding:utf-8 -*-
From _ future _ import absolute_import, print_function, division, with_statementdef loop1 (): "" loop 1 throws a function and corresponding parameters, and receive 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 outputs the result" while True: func, args = 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 preceding example, loop1 is responsible for generating the task, loop2 is responsible for executing the task, the main cycle is responsible for scheduling the task, and the task result is sent back to the task executor.
How Tornado works

First, let's look at an example of using Tornado coroutine Asynchronization.

#!/usr/bin/env python# -*- coding:utf-8 -*-from __future__ import absolute_import, print_function, division, with_statementfrom tornado import genfrom tornado import webfrom tornado import httpclientclass ActionHandler(web.RequestHandler):  @gen.coroutine  def get(self):    response = yield httpclient.AsyncHTTPClient().fetch("http://www.linuxzen.com")    # ...

In fact, the principle has been clearly explained in the above simple example. Let's analyze the above example. First, Tornado gets ActionHandler. the get method throws a task of (next) and executes the task asynchronously. When the task (Network request) ends or an exception occurs, Tornado obtains the Event Notification and then places the result back (send) in this method, let the method continue to be executed.

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

At this time, our method is actually the loop1 function in the previous example, and Tornado schedules and executes the task it throws.
Summary

The coroutine Asynchronization of Tornado can make Asynchronization appear to be executed sequentially, and can be freed from a large string of callback.

The coroutine Asynchronization of Tornado is not clear in these three languages. There are complicated encapsulation and transmission. If you are interested, you can read the source code by yourself.

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.