Tornado high concurrency Processing Method Instance code, tornado Processing Method
This article mainly shares an example of Tornado high concurrency processing method, which is as follows:
#! /Bin/env python #-*-coding: UTF-8-*-import tornado. httpserverimport tornado. ioloopimport tornado. optionsimport tornado. webimport tornado. genfrom tornado. concurrent import run_on_executorfrom concurrent. futures import ThreadPoolExecutorimport timefrom tornado. options import define, optionsdefine ("port", default = 8000, help = "run on the given port", type = int) class SleepHandler (tornado. web. requestHandler): executor = ThreadPoolExecutor (2) @ tornado. web. asynchronous @ tornado. gen. coroutine def get (self): # If the asynchronous call you execute will return a value that will be called again, this can be done (just for demonstration). Otherwise, yield will do res = yield self directly. sleep () self. write ("when I sleep % s" % res) self. finish () @ run_on_executor def sleep (self): time. sleep (5) return 5 class JustNowHandler (tornado. web. requestHandler): def get (self): self. write ("I hope just now see you") if _ name _ = "_ main _": tornado. options. parse_command_line () app = tornado. web. application (handlers = [(r "/sleep", SleepHandler), (r "/justnow", JustNowHandler)]) http_server = tornado. httpserver. HTTPServer (app) http_server.listen (options. port) tornado. ioloop. IOLoop. instance (). start ()
Summary
The above is all about the code of the Tornado high concurrency Processing Method Instance. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!