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.
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.
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.
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. _
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")
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]
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.
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.
#使用列表方式传递多个异步调用 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.
#使用列表方式传递多个异步调用 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!