Home  >  Article  >  Backend Development  >  Detailed explanation of the use of Tornado coroutines in Python (with examples)

Detailed explanation of the use of Tornado coroutines in Python (with examples)

不言
不言forward
2018-10-16 16:03:533902browse
This article brings you a detailed explanation of the use of Tornado coroutines in Python (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Using Tornado coroutines can develop asynchronous behavior similar to synchronous code. At the same time, because the coroutine itself does not use threads, it reduces the overhead of thread context switching and is an efficient development model.

1. Write coroutine function

Example: Using coroutine technology to develop web page access function

#用协程技术开发网页访问功能
from tornado import  gen #引入协程库gen
from tornado.httpclient import AsyncHTTPClient
import time

#使用gen.coroutine修饰器
@gen.coroutine
def coroutine_visit():
    http_client=AsyncHTTPClient()
    response=yield http_client.fetch("http://www.baidu.com")
    print(response.body)

In this example, the asynchronous client AsyncHTTPClient is still used for page access. The decorator @gen.coroutine declares that this is a coroutine function. Due to the yield keyword, there is no need to write a callback function in the code to process the access results. Instead, the result processing statement can be written directly after the yield statement.

2. Call the coroutine function

Since the Tornado coroutine is implemented based on Python's yield keyword, it cannot be called directly like an ordinary function.
Coroutine functions can be called in the following three ways:

  • Called through the yield keyword within a function that is itself a coroutine.

  • When IOLoop has not started, call it through the run_sync() function of IOLoop.

  • When IOLoop has been started, it is called through the spawn_callback() function of IOLoop.

Example: Call the coroutine function through the coroutine function

Code:

#用协程技术开发网页访问功能
from tornado import  gen #引入协程库gen
from tornado.httpclient import AsyncHTTPClient
import time

#使用gen.coroutine修饰器
@gen.coroutine
def coroutine_visit():
    http_client=AsyncHTTPClient()
    response=yield http_client.fetch("http://www.baidu.com")
    print(response.body)

@gen.coroutine
def outer_coroutine():
    print("start call coroutine_visit")
    yield coroutine_visit()
    print("end call coroutine_cisit")

In this example, outer_coroutine() and coroutine_visit() are both coroutine functions Program functions, so they can be called through the yield keyword. _

Example: When IOLoo has not been started, call it through the run_sync() function of IOLoop.
IOLoop is the main event loop object of Tornado, through which the Tornado program listens to access requests from external clients and performs corresponding operations.

Code:

#用协程技术开发网页访问功能
from tornado import  gen #引入协程库gen
from tornado.httpclient import AsyncHTTPClient
from tornado.ioloop import IOLoop  #引入IOLoop对象

#使用gen.coroutine修饰器
@gen.coroutine
def coroutine_visit():
    http_client=AsyncHTTPClient()
    response=yield http_client.fetch("http://www.baidu.com")
    print(response.body)

def func_normal():
    print("start call coroutine_visit")
    IOLoop.current().run_sync(lambda :coroutine_visit())
    print("end call coroutine_visit")
When the program has not entered the running state of IOLoop, the coroutine function can be called through the run_sync() function.

Note: The run_sync() function will block the call of the current function until the execution of the called coroutine is completed.

In fact, Tornado requires that the coroutine function can be called in the running state of IOLoop, but the run_sync function automatically completes the steps of starting and stopping IOLoop. Its implementation logic is:

[Start IOLoop]》[Call the coroutine function encapsulated by lambda]》[Stop IOLoop]

Example: When IOLoop is started, call through the spawn_callback() function

Code:

#用协程技术开发网页访问功能
from tornado import  gen #引入协程库gen
from tornado.httpclient import AsyncHTTPClient
from tornado.ioloop import IOLoop  #引入IOLoop对象

#使用gen.coroutine修饰器
@gen.coroutine
def coroutine_visit():
    http_client=AsyncHTTPClient()
    response=yield http_client.fetch("http://www.baidu.com")
    print(response.body)

def func_normal():
    print("start call coroutine_visit")
    IOLoop.current().spawn_callback(coroutine_visit)
    print("end call coroutine_visit")

The spawn_callback() function will not wait for the execution of the called coroutine to be completed. All the upper and lower print statements will be completed immediately, and coroutine__visit itself will be called by IOLoop at the appropriate time.

Note: IOLoop's spawn_callback() function does not provide developers with a method to obtain the return value of a coroutine function call, so span_callback() can only be used to call a coroutine function without a return value.

3. Call the blocking function in the coroutine

Directly calling the blocking function in the coroutine will affect the performance of the coroutine itself, so Tornado provides the use of thread pools to schedule blocking functions in the coroutine, thus Methods that do not affect the continued execution of the coroutine itself.

Code example:

from concurrent.futures import ThreadPoolExecutor
from tornado import gen

#定义线程池
thread_pool=ThreadPoolExecutor(2)

def mySleep(count):
    import time
    for x in range(count):
        time.sleep(1)

@gen.coroutine
def call_blocking():
    print("start")
    yield thread_pool.submit(mySleep,10)
    print("end")

The code first references the ThreadPoolExecutor class of concurrent.futures and instantiates a thread pool thread_pool consisting of two threads. In the coroutine call_blocking that needs to call a blocking function, use thread_pool.submit to call the blocking function and return it through yield. This will not block the continued execution of the thread where the coroutine is located, and also ensure the execution order of the code before and after the blocking function.

4. Waiting for multiple asynchronous calls in the coroutine

So far, we know the programming method of waiting for an asynchronous call with a yield keyword in the coroutine. In fact, Tornado allows you to use a yield keyword in a coroutine to wait for multiple asynchronous calls. You only need to pass these calls to the yield keyword in the form of a list or dictionary.
Example: Use list method to pass multiple asynchronous calls
#使用列表方式传递多个异步调用
from tornado import gen  #引入协程库gen
from tornado.httpclient import AsyncHTTPClient

@gen.coroutine   #使用gen.coroutine修饰器
def coroutine_visit():
    http_client=AsyncHTTPClient()
    list_response=yield [
        http_client.fetch("http://www.baidu.com"),
        http_client.fetch("http://www.api.jiutouxiang.com")
    ]
    for response in list_response:
        print(response.body)

Still use @gen.coroutine decorator to define coroutine in the code, and use list to pass several where yield is required An asynchronous call, yield will return and continue execution only after all calls in the list are completed. yield returns the call results in a list.

Example: Pass multiple asynchronous calls in dictionary mode:
#使用列表方式传递多个异步调用
from tornado import gen  #引入协程库gen
from tornado.httpclient import AsyncHTTPClient

@gen.coroutine   #使用gen.coroutine修饰器
def coroutine_visit():
    http_client=AsyncHTTPClient()
    dict_response=yield {
       "baidu": http_client.fetch("http://www.baidu.com"),
        "9siliao":http_client.fetch("http://www.api.jiutouxiang.com")
    }
    print(dict_response["baidu"].body)

The above is the detailed content of Detailed explanation of the use of Tornado coroutines in Python (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete