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í)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
關(guān)于AnacondaNavigator?Jupyter?Notebook更換Python內(nèi)核的問(wèn)題
因?yàn)樾掳惭b的Anaconda?Navigator默認(rèn)安裝了一個(gè)Python,Jupyter?Notebook默認(rèn)使用的內(nèi)核就是這個(gè)Python,跟我系統(tǒng)安裝好的Python沖突了,下面小編給大家介紹AnacondaNavigator?Jupyter?Notebook更換Python內(nèi)核的問(wèn)題,需要的朋友可以參考下2022-02-02python內(nèi)置函數(shù):lambda、map、filter簡(jiǎn)單介紹
Python 內(nèi)置了一些比較特殊且實(shí)用的函數(shù),使用這些能使你的代碼簡(jiǎn)潔而易讀。下面對(duì)python內(nèi)置函數(shù):lambda、map、filter簡(jiǎn)單介紹下,需要的朋友參考下吧2017-11-11Python使用pandas導(dǎo)入xlsx格式的excel文件內(nèi)容操作代碼
這篇文章主要介紹了Python使用pandas導(dǎo)入xlsx格式的excel文件內(nèi)容,基本導(dǎo)入是在Python中使用pandas導(dǎo)入.xlsx文件的方法是read_excel(),本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12一文掌握Python爬蟲(chóng)XPath語(yǔ)法
這篇文章主要介紹了一文掌握Python爬蟲(chóng)XPath語(yǔ)法,xpath是一門(mén)在XML和HTML文檔中查找信息的語(yǔ)言,可用來(lái)在XML和HTML文檔中對(duì)元素和屬性進(jìn)行遍歷,XPath 通過(guò)使用路徑表達(dá)式來(lái)選取 XML 文檔中的節(jié)點(diǎn)或者節(jié)點(diǎn)集。下面會(huì)更學(xué)習(xí)的介紹,需要的朋友可以參考一下2021-11-11Pytorch GPU顯存充足卻顯示out of memory的解決方式
今天小編就為大家分享一篇Pytorch GPU顯存充足卻顯示out of memory的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01tensorflow使用tf.data.Dataset 處理大型數(shù)據(jù)集問(wèn)題
這篇文章主要介紹了tensorflow使用tf.data.Dataset 處理大型數(shù)據(jù)集問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12python使用OpenCV獲取高動(dòng)態(tài)范圍成像HDR
這篇文章主要介紹了python使用OpenCV獲取高動(dòng)態(tài)范圍成像HDR,如何使用不同曝光設(shè)置拍攝的多張圖像創(chuàng)建高動(dòng)態(tài)范圍圖像HDR,下文嗎更詳細(xì)的內(nèi)容介紹,需要的小伙伴可以參考一下2022-04-04