1. Why to use asynchronous Web services
Concurrent processing is more efficient with asynchronous non-blocking requests.
2. Synchronization vs. asynchronous request comparison
When a request is synchronized, the Web server process is blocked, which means that when a request is processed, the server process is suspended until the request is completed.
When an asynchronous request is made, the Web server process waits for the request to be processed, letting the I/O loop open so that it serves other requests, resumes execution of the callback function or generator after the request processing completes, and no longer waits for the pending request to suspend the process. The entire process is asynchronous.
3. Synchronous and asynchronous request examples
Sync Request:
class Indexhandler (Tornado.web.RequestHandler): def Get (self): client=tornado.httpclient.HTTPClient () response=client.fetch (" http://test.com/list " ) self.write ("success")
Asynchronous Request:
class Indexasynchandler (Tornado.web.RequestHandler): @tornado. web.asynchronous def Get ( Self): client=tornado.httpclient.AsyncHTTPClient () client.fetch ("http:// Test.com/list", callback=self.on_response) def on_response (self , response): self.write ("success") self.finish ()
Routing configuration:
(R'/test', Test_async. Indexhandler), (R'/testasync', Test_async. Indexasynchandle)
Stress tests are performed using the Http_load tool (see "Http_load Use Details" http://www.cnblogs.com/shijingjing07/p/6539179.html for Http_load) with the following results:
Synchronous pressure test:
[[email protected] http_load-12mar2006] # ./http_load-p 100-s URL in seconds7 mean bytes/connection0.45 fetches/sec 3.15 bytes/secmsecs/connect : 0.113037 mean, 0.258 max, 0.021 minmsecs/first-response:31186.5 mean, 59721.3 max, 2246.32200--27
Asynchronous Stress test:
in 60.0046 seconds7 mean bytes/connection3.48306 fetches/sec, 24.3814 bytes/secmsecs /connect:0.0944641 mean, 0.387 max, 0.021 minmsecs/first-response:20088 mean, 30650 max, 10601.1 200--209
The comparison shows that in 60s time, the number of concurrent requests is 100,
The sync request responds with only 27 requests, while the asynchronous request reaches 209
4. Instructions for using asynchronous requests
The synchronization request closes the connection automatically after the request is complete.
The asynchronous request keeps the connection open and requires a manual shutdown of the connection.
The role of the @tornado.web.asynchronous adorner in tornado is to keep the connection open,
After the callback function finishes executing, call the Finish method to actively close the connection.
5. Asynchronous generators
In the above example, the callback function is used to do the business processing and closing the connection.
The disadvantage of a callback function is that it can cause a callback abyss and the system will be difficult to maintain. Called callbacks, such as callbacks.
defGet (self): client=asynchttpclient () Client.fetch ("http://example.com", callback=on_response)defOn_response (Self, Response): Client=asynchttpclient () Client.fetch ("http://another.example.com/", callback=on_response2)defOn_response2 (Self, Response): Client=asynchttpclient () Client.fetch ("http://still.another.example.com/", callback=on_response3)defon_response3 (Self, Response): [etc, etc.]
tornado2.1 introduces the Tornado.gen module, which makes it more uncluttered to perform asynchronous requests.
Asynchronous Request:
class Indexgenhandler (Tornado.web.RequestHandler): @tornado. web.asynchronous @tornado. Gen.engine def Get (self): client=tornado.httpclient.AsyncHTTPClient () Response= yield tornado.gen.Task (Client.fetch,"http://test.com/list") self.write ("success") self.finish ()
Routing configuration:
(R'/testgen', Test_async. Indexgenhandler),
Asynchronous Stress test:
in 60.0055 seconds7 mean bytes/connection3.44968 fetches/sec, 24.1478 bytes/secmsecs /connect:0.113483 mean, 0.948 max, 0.024 minmsecs/first-response:20156.5 mean, 32294.2 max, 9607.34
200--207
Tornado.gen is a generator (see "Python Builder, Function, array" http://www.cnblogs.com/shijingjing07/p/6478539.html) for the generator,
The yield keyword functions as a return control, and after the asynchronous task is executed, the program resumes at the yield location.
You can see that using the generator, after asynchronous, the business process is not done in the callback function, it looks like synchronous processing, the code logic is clearer.
Asynchronous requests using generators and callback functions are the same.
6. Scenarios for asynchronous requests
Asynchronous requests can significantly increase the efficiency of concurrent requests when the request processing logic is complex and time-consuming, or when the database is being requested for long periods.
At the same time, the combination of caching, business logic on the client and other means to alleviate the server pressure.
Tornado Asynchronous Web Requests