Home >Backend Development >Python Tutorial >Introduction to Tornado's synchronous and asynchronous I/O in python (with examples)

Introduction to Tornado's synchronous and asynchronous I/O in python (with examples)

不言
不言forward
2018-10-11 14:45:512226browse

This article brings you an introduction to Tornado’s synchronous and asynchronous I/O in Python (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.

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 processing and storage of data or The pace of network requests (I/O operations) is inconsistent. At this time, 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 of 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 Introduction to Tornado's synchronous and asynchronous I/O 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