在 AWS Elastic Beanstalk 上部署 Tornado 应用程序
Deploying Tornado app on AWS Elastic Beanstalk
我有一个用 Python 2.7/Tornado 编写的服务器,我正在尝试将它部署到 AWS 上。
我遇到了 AWS Elastic Beanstalk,这看起来是部署我的代码的一种非常方便的方法。
我完成了这个 tutorial 并且能够部署 Flask 示例应用程序。
但是,我不知道如何部署如下所示的测试龙卷风应用程序。
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()
我认为您的问题与以下事实有关:Elastic Beanstalk 使用 WSGI 为 Python Web 应用程序提供服务,而 Tornado 的服务器不符合 WSGI。在通过 WSGI 提供应用程序之前,您可能想尝试将您的应用程序包装在 WSGI adapter 中。
除非您依赖 Tornado 的异步功能,否则这应该可以正常工作,因为 WSGI 是严格同步的。
您可以使用 WSGI 部署 tornado 应用程序mod
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)
我有一个用 Python 2.7/Tornado 编写的服务器,我正在尝试将它部署到 AWS 上。 我遇到了 AWS Elastic Beanstalk,这看起来是部署我的代码的一种非常方便的方法。
我完成了这个 tutorial 并且能够部署 Flask 示例应用程序。 但是,我不知道如何部署如下所示的测试龙卷风应用程序。
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()
我认为您的问题与以下事实有关:Elastic Beanstalk 使用 WSGI 为 Python Web 应用程序提供服务,而 Tornado 的服务器不符合 WSGI。在通过 WSGI 提供应用程序之前,您可能想尝试将您的应用程序包装在 WSGI adapter 中。
除非您依赖 Tornado 的异步功能,否则这应该可以正常工作,因为 WSGI 是严格同步的。
您可以使用 WSGI 部署 tornado 应用程序mod
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)