Home  >  Article  >  Backend Development  >  Tornado synchronous and asynchronous I/O example code explanation in Python

Tornado synchronous and asynchronous I/O example code explanation in Python

不言
不言forward
2018-10-16 16:07:492189browse
This article brings you an explanation of the Tornado synchronization and asynchronous I/O example code in Python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Coroutine is a recommended programming method in Tornado. Using coroutine can develop simple and efficient asynchronous processing code.

Comparison of synchronous and asynchronous I/O

As we all know, the operating efficiency of the CPU is higher than disk storage and higher than network requests, which leads to the CPU's inefficiency in data processing. The processing is inconsistent with the pace of data storage or network requests (I/O operations). In this case, you can choose synchronous or asynchronous I/O operations.

Synchronous I/O operations will cause the process to block until the I/O operation is completed;

Asynchronous I/O operations will not cause the requesting process to block.

Simple code example for Tornado synchronous I/O:

Code:

#导入Tornado的HTTP客户端
from tornado.httpclient import HTTPClient

def synchronous_visit():
    http_client=HTTPClient()
    #阻塞,知道对网址访问完成
    respone=http_client.fetch("http://www.baidu.com")
    print(respone.body)
synchronous_visit()

HTTPClient is Tornato's synchronous access HTTP client. The synchronous_visit() function in the above code uses a typical synchronous I/O operation to access the URL. The execution time of this function depends on the network speed and the response speed of the other party's server. Only when the access is completely completed and the result is obtained, the function can Execution complete.

Simple code example of Tornado asynchronous I/O:

from tornado.httpclient import AsyncHTTPClient
def handle_response(response):
    print(response.body)

def asyncronous_visit():
    http_client=AsyncHTTPClient()
    http_client.fetch("http://www.baoidu.com",callback=handle_response)

AsyncHTTPClient is Tornado's asynchronous access HTTP client. In the asynchronous_visit() function in the above code, AsyncHTTPClient is used to asynchronously access the third-party website. The http_client.fetch() function will return immediately after the call without waiting for the actual access to be completed, resulting in asynchronous_visit() also being executed immediately. Finish. When the access to the URL is actually completed, AsyncHTTPClient will call the function specified by the callback parameter, in which the access results can be processed.

The above is the detailed content of Tornado synchronous and asynchronous I/O example code explanation in Python. 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