问题
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