[英]Serving Pyramid application using twistd
我有一個Pyramid應用程序,其中還包含一些Twisted代碼,因此我想使用Twisted用一塊石頭殺死兩只鳥來提供服務。
這是我的.tac文件:
from twisted.web.server import Site
from twisted.web.wsgi import WSGIResource
from twisted.internet import reactor
from twisted.application import internet, service
import os
from pyramid.paster import get_app, setup_logging
config='/path/to/app/production.ini'
config = os.path.abspath(config)
port = 8888
application = get_app(config, 'main')
# Twisted WSGI server setup...
resource = WSGIResource(reactor, reactor.getThreadPool(), application)
factory = Site(resource)
service = internet.TCPServer(port, factory)
service.setServiceParent(application)
我使用以下命令來運行它:
twistd -y myApp.tac
我收到錯誤消息,告訴我get_app()方法不會返回可以這種方式使用的對象。 例如:
"Failed to load application: 'PrefixMiddleware' object has no attribute 'addService'"
使用twisted運行金字塔應用程序的最佳方法是什么?
您可以在Twisted Web的twistd
插件中使用WSGI支持,以縮短此時間並使其更易於配置。 創建一個像這樣的模塊:
from pyramid.paster import get_app
config = '/path/to/app/production.ini'
myApp = get_app(config, 'main')
然后像這樣運行twistd
:
$ twistd web --port tcp:8888 --wsgi foo.myApp
其中foo
是您創建的模塊的名稱。
我找到了可行的解決方案。 這是工作的.tac文件:
from twisted.web.server import Site
from twisted.web.wsgi import WSGIResource
from twisted.internet import reactor
from twisted.application import internet, service
import os
from pyramid.paster import get_app, setup_logging
config='/path/to/app/production.ini'
config = os.path.abspath(config)
port = 8888
# Get the WSGI application
myApp = get_app(config, 'main')
# Twisted WSGI server setup
resource = WSGIResource(reactor, reactor.getThreadPool(), myApp)
factory = Site(resource)
# Twisted Application setup
application = service.Application('mywebapp')
internet.TCPServer(port, factory).setServiceParent(application)
get_app()獲取Pyramid WSGI應用程序,而internet.TCPServer需要Twisted Application對象,因此不要混淆這些對象。
此代碼將在TCP端口8888上啟動應用程序。
如果有人有更好/更清晰的方法來實施此操作,請添加您的答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.