溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python tornado怎樣啟動(dòng)和配置

發(fā)布時(shí)間:2020-11-10 09:38:20 來(lái)源:億速云 閱讀:173 作者:小新 欄目:編程語(yǔ)言

這篇文章主要介紹python tornado怎樣啟動(dòng)和配置,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

如果小伙伴一直關(guān)注這個(gè)系列,那么第一步應(yīng)該對(duì)你來(lái)說(shuō)習(xí)以為常。

$ mkdir tornado_todo
$ cd tornado_todo
$ pipenv install --python 3.6
$ pipenv shell
(tornado-someHash) $ pipenv install tornado

創(chuàng)建一個(gè) setup.py 文件來(lái)安裝我們的應(yīng)用程序相關(guān)的東西:

(tornado-someHash) $ touch setup.py
# setup.py
from setuptools import setup, find_packages
 
requires = [
    'tornado',
    'tornado-sqlalchemy',
    'psycopg2',
]
 
setup(
    name='tornado_todo',
    version='0.0',
    description='A To-Do List built with Tornado',
    author='<Your name>',
    author_email='<Your email>',
    keywords='web tornado',
    packages=find_packages(),
    install_requires=requires,
    entry_points={
        'console_scripts': [
            'serve_app = todo:main',
        ],
    },
)

因?yàn)?Tornado 不需要任何外部配置,所以我們可以直接編寫 Python 代碼來(lái)讓程序運(yùn)行。讓我們創(chuàng)建 todo 目錄,并用需要的前幾個(gè)文件填充它。

todo/
    __init__.py
    models.py
views.py

 就像 Flask 和 Pyramid 一樣,Tornado 也有一些基本配置,放在 __init__.py 中。從 tornado.web 中,我們將導(dǎo)入 Application 對(duì)象,它將處理路由和視圖的連接,包括數(shù)據(jù)庫(kù)(當(dāng)我們談到那里時(shí)再說(shuō))以及運(yùn)行 Tornado 應(yīng)用程序所需的其它額外設(shè)置。

# __init__.py
from tornado.web import Application
 
def main():
    """Construct and serve the tornado application."""
app = Application()

像 Flask 一樣,Tornado 主要是一個(gè) DIY 框架。當(dāng)構(gòu)建我們的 app 時(shí),我們必須設(shè)置該應(yīng)用實(shí)例。因?yàn)?Tornado 用它自己的 HTTP 服務(wù)器來(lái)提供該應(yīng)用,我們必須設(shè)置如何提供該應(yīng)用。首先,在 tornado.options.define 中定義要監(jiān)聽(tīng)的端口。然后我們實(shí)例化 Tornado 的 HTTPServer,將該 Application 對(duì)象的實(shí)例作為參數(shù)傳遞給它。

# __init__.py
from tornado.httpserver import HTTPServer
from tornado.options import define, options
from tornado.web import Application
 
define('port', default=8888, help='port to listen on')
 
def main():
    """Construct and serve the tornado application."""
    app = Application()
    http_server = HTTPServer(app)
http_server.listen(options.port)

當(dāng)我們使用 define 函數(shù)時(shí),我們最終會(huì)在 options 對(duì)象上創(chuàng)建屬性。第一個(gè)參數(shù)位置的任何內(nèi)容都將是屬性的名稱,分配給 default 關(guān)鍵字參數(shù)的內(nèi)容將是該屬性的值。

例如,如果我們將屬性命名為 potato 而不是 port,我們可以通過(guò) options.potato 訪問(wèn)它的值。

在 HTTPServer 上調(diào)用 listen 并不會(huì)啟動(dòng)服務(wù)器。我們必須再做一步,找一個(gè)可以監(jiān)聽(tīng)請(qǐng)求并返回響應(yīng)的工作應(yīng)用程序,我們需要一個(gè)輸入輸出循環(huán)。幸運(yùn)的是,Tornado tornado.ioloop.IOLoop 的形式提供了開(kāi)箱即用的功能。

# __init__.py
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.options import define, options
from tornado.web import Application
 
define('port', default=8888, help='port to listen on')
 
def main():
    """Construct and serve the tornado application."""
    app = Application()
    http_server = HTTPServer(app)
    http_server.listen(options.port)
    print('Listening on http://localhost:%i' % options.port)
IOLoop.current().start()

我喜歡某種形式的 print 語(yǔ)句,來(lái)告訴我什么時(shí)候應(yīng)用程序正在提供服務(wù),這是我的習(xí)慣。如果你愿意,可以不使用 print。

我們以IOLoop.current().start() 開(kāi)始我們的 I/O 循環(huán)。讓我們進(jìn)一步討論輸入,輸出和異步性。

以上是python tornado怎樣啟動(dòng)和配置的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI