Tornado高并發(fā)處理方法實(shí)例代碼
本文主要分享的是一則關(guān)于Tornado高并發(fā)處理方法的實(shí)例,具體如下:
#!/bin/env python
# -*- coding:utf-8 -*-
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.gen
from tornado.concurrent import run_on_executor
from concurrent.futures import ThreadPoolExecutor
import time
from tornado.options import define, options
define("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):
# 假如你執(zhí)行的異步會(huì)返回值被繼續(xù)調(diào)用可以這樣(只是為了演示),否則直接yield就行
res = yield self.sleep()
self.write("when i sleep %s 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()
總結(jié)
以上就是本文關(guān)于Tornado高并發(fā)處理方法實(shí)例代碼的全部內(nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
Python 實(shí)現(xiàn)一行輸入多個(gè)數(shù)字(用空格隔開)
這篇文章主要介紹了Python 實(shí)現(xiàn)一行輸入多個(gè)數(shù)字,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Python實(shí)現(xiàn)i人事自動(dòng)打卡的示例代碼
這篇文章主要介紹了Python實(shí)現(xiàn)i人事自動(dòng)打卡的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
Python基于數(shù)列實(shí)現(xiàn)購物車程序過程詳解
這篇文章主要介紹了Python基于數(shù)列實(shí)現(xiàn)購物車程序過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
對(duì)numpy中array和asarray的區(qū)別詳解
下面小編就為大家分享一篇對(duì)numpy中array和asarray的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04
python中的多線程鎖lock=threading.Lock()使用方式
這篇文章主要介紹了python中的多線程鎖lock=threading.Lock()使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Python使用微信SDK實(shí)現(xiàn)的微信支付功能示例
這篇文章主要介紹了Python使用微信SDK實(shí)現(xiàn)的微信支付功能,結(jié)合實(shí)例形式分析了Python調(diào)用微信SDK接口實(shí)現(xiàn)微信支付功能的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-06-06

