亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python tornado開(kāi)啟多進(jìn)程的幾種方法

 更新時(shí)間:2023年04月16日 11:12:41   作者:終成一個(gè)大象  
本文主要介紹了python tornado開(kāi)啟多進(jìn)程的幾種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

以下各種方式僅供參考,本人親測(cè)只有官方提供的方式比較靠譜。

1. 使用多個(gè)進(jìn)程啟動(dòng)多個(gè)Tornado實(shí)例

import tornado.httpserver
import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
? ? def get(self):
? ? ? ? self.write("Hello, world")

if __name__ == "__main__":
? ? app = tornado.web.Application([(r"/", MainHandler)])
? ? server = tornado.httpserver.HTTPServer(app)
? ? server.bind(8888)
? ? server.start(0) ?# 0 表示啟動(dòng)與CPU數(shù)量相同的進(jìn)程
? ? tornado.ioloop.IOLoop.current().start()

2. 使用tornado.process.fork_processes()方法啟動(dòng)多個(gè)進(jìn)程

import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.process

class MainHandler(tornado.web.RequestHandler):
? ? def get(self):
? ? ? ? self.write("Hello, world")

if __name__ == "__main__":
? ? app = tornado.web.Application([(r"/", MainHandler)])
? ? server = tornado.httpserver.HTTPServer(app)
? ? server.bind(8888)
? ? tornado.process.fork_processes(2) #

tornado.process.fork_processes(2) 表示啟動(dòng)2個(gè)進(jìn)程,每個(gè)進(jìn)程都會(huì)調(diào)用 server.start(0) 來(lái)啟動(dòng)Tornado實(shí)例。注意:在使用 tornado.process.fork_processes() 啟動(dòng)多進(jìn)程時(shí),需要在 if __name__ == "__main__": 中調(diào)用該方法,否則會(huì)出現(xiàn)錯(cuò)誤。

完整代碼如下:

import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.process

class MainHandler(tornado.web.RequestHandler):
? ? def get(self):
? ? ? ? self.write("Hello, world")

if __name__ == "__main__":
? ? app = tornado.web.Application([(r"/", MainHandler)])
? ? server = tornado.httpserver.HTTPServer(app)
? ? server.bind(8888)
? ? tornado.process.fork_processes(2)
? ? server.start(0)
? ? tornado.ioloop.IOLoop.current().start()

3.使用標(biāo)準(zhǔn)庫(kù)中的multiprocessing

除了以上提到的方式,還可以使用Python標(biāo)準(zhǔn)庫(kù)中的multiprocessing模塊來(lái)啟動(dòng)多個(gè)Tornado進(jìn)程,具體實(shí)現(xiàn)可以參考以下示例代碼:

import tornado.httpserver
import tornado.ioloop
import tornado.web
from multiprocessing import Process

class MainHandler(tornado.web.RequestHandler):
? ? def get(self):
? ? ? ? self.write("Hello, world")

def start_tornado():
? ? app = tornado.web.Application([(r"/", MainHandler)])
? ? server = tornado.httpserver.HTTPServer(app)
? ? server.listen(8888)
? ? tornado.ioloop.IOLoop.current().start()

if __name__ == "__main__":
? ? processes = []
? ? for i in range(2):
? ? ? ? p = Process(target=start_tornado)
? ? ? ? p.start()
? ? ? ? processes.append(p)
? ? for p in processes:
? ? ? ? p.join()

這段代碼會(huì)啟動(dòng)兩個(gè)Tornado進(jìn)程,每個(gè)進(jìn)程都會(huì)監(jiān)聽(tīng)8888端口,并使用單獨(dú)的進(jìn)程處理請(qǐng)求。

4.使用第三方模塊gevent

還有一個(gè)方式是使用第三方模塊gevent來(lái)實(shí)現(xiàn)協(xié)程并發(fā),配合Tornado使用可以達(dá)到類(lèi)似多進(jìn)程的效果,但是只使用一個(gè)進(jìn)程。示例代碼如下:

import gevent.monkey
gevent.monkey.patch_all()

import tornado.httpserver
import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
? ? def get(self):
? ? ? ? self.write("Hello, world")

if __name__ == "__main__":
? ? app = tornado.web.Application([(r"/", MainHandler)])
? ? server = tornado.httpserver.HTTPServer(app)
? ? server.bind(8888)
? ? server.start(0) ?# 0 表示啟動(dòng)與CPU數(shù)量相同的進(jìn)程
? ? tornado.ioloop.IOLoop.current().start()

在上面的代碼中,我們引入了gevent.monkey模塊,使用patch_all()方法將所有的阻塞式IO替換為非阻塞式IO,然后在啟動(dòng)Tornado時(shí),使用server.start(0)方法啟動(dòng)與CPU數(shù)量

5.使用官方提供方式

listen:?jiǎn)芜M(jìn)程:

    async def main():
        server = HTTPServer()
        server.listen(8888)
        await asyncio.Event.wait()
    
    asyncio.run(main())

在許多情況下,tornado.web.Application.listen可用于避免明確創(chuàng)建HTTPServer的需要。

雖然此示例不會(huì)單獨(dú)創(chuàng)建多個(gè)進(jìn)程,但當(dāng)thereusereuse_port=True參數(shù)傳遞給listen()時(shí),您可以多次運(yùn)行程序以創(chuàng)建多進(jìn)程服務(wù)。

add_sockets:多過(guò)程:

    sockets = bind_sockets(8888)
    tornado.process.fork_processes(0)
    async def post_fork_main():
        server = HTTPServer()
        server.add_sockets(sockets)
        await asyncio.Event().wait()
    asyncio.run(post_fork_main())

add_sockets接口更復(fù)雜,但它可以與tornado.process.fork_processes一起使用,以運(yùn)行從單父分支的所有工作進(jìn)程的多進(jìn)程服務(wù)。如果您想以bind_sockets以外的某種方式創(chuàng)建監(jiān)聽(tīng)套接字,add_sockets也可以在單進(jìn)程服務(wù)器中使用。

請(qǐng)注意,使用此模式時(shí),觸及事件循環(huán)的任何東西都不能在fork_processes之前運(yùn)行。

bind/start:簡(jiǎn)單不建議使用的多進(jìn)程:

    server = HTTPServer()
    server.bind(8888)
    server.start(0)  # Forks multiple sub-processes
    IOLoop.current().start()

此模式被棄用,因?yàn)樗枰訮ython 3.10以來(lái)被棄用的asyncio模塊中的接口。在start方法中創(chuàng)建多個(gè)進(jìn)程的支持將在的未來(lái)版本中刪除。

此模式就是文中所說(shuō)的第一種模式,單從官方文檔來(lái)看,這種方式已經(jīng)被拋棄,本人在實(shí)測(cè)中也發(fā)現(xiàn)存在問(wèn)題:無(wú)法完全關(guān)閉fork的子進(jìn)程。

6.使用supervisor

使用supervisor等進(jìn)程管理工具來(lái)管理多個(gè)Tornado進(jìn)程,這種方式可以更加方便地監(jiān)控和管理多個(gè)進(jìn)程,不過(guò)需要額外的配置和安裝進(jìn)程管理工具。

到此這篇關(guān)于python tornado開(kāi)啟多進(jìn)程的幾種方法的文章就介紹到這了,更多相關(guān)python tornado多進(jìn)程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python的字典和集合你了解嗎

    python的字典和集合你了解嗎

    章主要為大家詳細(xì)介紹了python的字典和集合,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-02-02
  • 基于Python和TFIDF實(shí)現(xiàn)提取文本中的關(guān)鍵詞

    基于Python和TFIDF實(shí)現(xiàn)提取文本中的關(guān)鍵詞

    TFIDF 的工作原理是按比例增加一個(gè)詞語(yǔ)在文檔中出現(xiàn)的次數(shù),但會(huì)被它所在的文檔數(shù)量抵消。本文將利用TFIDF實(shí)現(xiàn)提取文本中的關(guān)鍵詞,感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧
    2022-04-04
  • Python實(shí)現(xiàn)正則表達(dá)式匹配任意的郵箱方法

    Python實(shí)現(xiàn)正則表達(dá)式匹配任意的郵箱方法

    今天小編就為大家分享一篇Python實(shí)現(xiàn)正則表達(dá)式匹配任意的郵箱方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • 利用Opencv實(shí)現(xiàn)圖片的油畫(huà)特效實(shí)例

    利用Opencv實(shí)現(xiàn)圖片的油畫(huà)特效實(shí)例

    這篇文章主要給大家介紹了關(guān)于利用Opencv實(shí)現(xiàn)圖片的油畫(huà)特效的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Python數(shù)據(jù)結(jié)構(gòu)與算法中的隊(duì)列詳解(2)

    Python數(shù)據(jù)結(jié)構(gòu)與算法中的隊(duì)列詳解(2)

    這篇文章主要為大家詳細(xì)介紹了Python中的隊(duì)列,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-03-03
  • 如何通過(guò)python代碼根據(jù)模板修改變量生成新yaml文件

    如何通過(guò)python代碼根據(jù)模板修改變量生成新yaml文件

    有些時(shí)候,需要根據(jù)一個(gè)yaml模板創(chuàng)建多個(gè)yaml文件實(shí)例,我們先寫(xiě)一個(gè)yaml文件模板,然后通過(guò)python代碼修改模板中的變量,存儲(chǔ)為一個(gè)新的yaml文件,需要配合python的庫(kù)Template及ymal使用,本文給大家講解的非常詳細(xì),需要的朋友跟隨小編一起看看吧
    2023-11-11
  • Python?time模塊時(shí)間獲取和轉(zhuǎn)換方法

    Python?time模塊時(shí)間獲取和轉(zhuǎn)換方法

    這篇文章主要介紹了Python?time模塊時(shí)間獲取和轉(zhuǎn)換,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • tensorflow實(shí)現(xiàn)加載mnist數(shù)據(jù)集

    tensorflow實(shí)現(xiàn)加載mnist數(shù)據(jù)集

    這篇文章主要為大家詳細(xì)介紹了tensorflow實(shí)現(xiàn)加載mnist數(shù)據(jù)集,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • Python執(zhí)行系統(tǒng)命令的五種方式小結(jié)

    Python執(zhí)行系統(tǒng)命令的五種方式小結(jié)

    在日常開(kāi)發(fā)中,有時(shí)需要在Python腳本中執(zhí)行系統(tǒng)命令,Python有五種方式來(lái)執(zhí)行系統(tǒng)命令(推薦使用第五種),本文為大家整理了這五種方法的具體使用,希望對(duì)大家有所幫助
    2024-01-01
  • tensorflow實(shí)現(xiàn)殘差網(wǎng)絡(luò)方式(mnist數(shù)據(jù)集)

    tensorflow實(shí)現(xiàn)殘差網(wǎng)絡(luò)方式(mnist數(shù)據(jù)集)

    這篇文章主要介紹了tensorflow實(shí)現(xiàn)殘差網(wǎng)絡(luò)方式(mnist數(shù)據(jù)集),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-05-05

最新評(píng)論