Flask deployed with twistd: Failed to load application: 'NoneType' object has no attribute 'startswith'

亡梦爱人 提交于 2020-01-07 00:52:53

问题


I am trying to deploy my Twisted application using .tac files and twistd

I tried to deploy it with the command line:

twistd -y service.tac

I have the error:

...

application = getApplication(self.config, passphrase)

--- <exception caught here> --- File "/usr/local/lib/python2.7/dist-packages/twisted/application/app.py", line 450, in getApplication application = service.loadApplication(filename, style, passphrase) File "/usr/local/lib/python2.7/dist-packages/twisted/application/service.py", line 411, in loadApplication passphrase) File "/usr/local/lib/python2.7/dist-packages/twisted/persisted/sob.py", line 224, in loadValueFromFile eval(codeObj, d, d) File "service.tac", line 54, in <module>

File "/usr/lib/python2.7/posixpath.py", line 61, in isabs return s.startswith('/')

exceptions.AttributeError: 'NoneType' object has no attribute 'startswith'

Failed to load application: 'NoneType' object has no attribute 'startswith'

My service.tac file is:

from flask import Flask app = Flask(__name__)


回答1:


Your application import_name can't be identified properly in *.tac file. If you create flask application in *.py file and import it in *.tac it will work just fine.

But you also need another list of instructions for deploying Flask application via twistd. Minimal example looks like this:

from twisted.application import internet, service
from twisted.web.server import Site
from twisted.web.wsgi import WSGIResource
from twisted.internet import reactor

from my_flask_module import my_flask_app

application = service.Application('myapplication')
service = service.IServiceCollection(application)

flask_resource = WSGIResource(reactor, reactor.getThreadPool(), my_flask_app)
flask_site = Site(flask_resource)
internet.TCPServer(8000, flask_site).setServiceParent(service)


来源:https://stackoverflow.com/questions/38775663/flask-deployed-with-twistd-failed-to-load-application-nonetype-object-has-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!