Skip to main content
 首页 » 编程设计

python之在 AWS Elastic Beanstalk 上部署 Tornado 应用程序

2024年10月01日7artech

我有一个用 Python 2.7/Tornado 编写的服务器,我正在尝试将它部署到 AWS 上。 我遇到了 AWS Elastic Beanstalk,它看起来是部署我的代码的一种非常方便的方法。

我经历了这个tutorial并能够部署 Flask 示例应用程序。 但是,我不知道如何部署如下所示的测试 Tornado 应用程序。

import tornado.web 
import tornado.ioloop 
 
class MainHandler(tornado.web.RequestHandler): 
    def get(self): 
        self.write("Hello, world") 
 
if __name__ == "__main__": 
    app = tornado.web.Application([ 
        (r"/.*", MainHandler), 
    ]) 
 
    app.listen(5000) 
    tornado.ioloop.IOLoop.current().start() 

当我尝试部署上述应用程序时,我的所有请求都导致错误 500,我不知道如何解决这个问题,因为我不知道 Flask 示例如何工作,但 Tornado 代码却没有。

requirements.txt 文件中有一个 tornado==4.4.2 的条目。

我尝试添加一些日志语句来写入外部文件,但文件没有创建,这可能意味着应用程序甚至没有启动。

如果有人可以提供一些在 AWS-EB 上部署 Tornado 应用程序的步骤,或者我应该如何开始对此进行故障排除,那就太好了。 如果我需要提供更多详细信息,请告诉我。

谢谢!

更新

在注意到 httpd error_log 文件、AWS 文档和 Berislav Lopac 的回答中的错误后,我找到了实现 Tornado 服务器的正确方法。 这是一个简单的服务器

import tornado.web 
import tornado.wsgi 
import tornado.ioloop 
 
class MainHandler(tornado.web.RequestHandler): 
    def get(self): 
        self.write("Hello, world") 
 
webApp = tornado.web.Application([ 
    (r"/", MainHandler), 
]) 
 
# Wrapping the Tornado Application into a WSGI interface 
# As per AWS EB requirements, the WSGI interface must be named 
# 'application' only 
application = tornado.wsgi.WSGIAdapter(webApp) 
 
if __name__ == '__main__': 
    # If testing the server locally, start on the specific port 
    webApp.listen(8080) 
    tornado.ioloop.IOLoop.current().start() 

附加链接: Tornado WSGI Documentation

请您参考如下方法:

您可以使用 WSGI mod 部署 tornado 应用程序

import tornado.web 
import tornado.wsgi 
 
class MainHandler(tornado.web.RequestHandler): 
    def get(self): 
        self.write("Hello, world") 
 
tornado_app = tornado.web.Application([ 
    (r"/", MainHandler), 
]) 
application = tornado.wsgi.WSGIAdapter(tornado_app) 

http://www.tornadoweb.org/en/stable/guide/running.html