Reputation: 1072
I have a Pyramid app that also has some Twisted code in it, and so I would like to serve the app using twistd to kill two birds with one stone.
Here is my .tac file:
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)
To run this I used:
twistd -y myApp.tac
I am getting errors telling me that the get_app() method does not return an object that can be used in this way. E.g.:
"Failed to load application: 'PrefixMiddleware' object has no attribute 'addService'"
What is the best way to run a Pyramid app using twistd?
Upvotes: 2
Views: 973
Reputation: 48335
You can use the WSGI support in Twisted Web's twistd
plugin to shorten this and make it more easily configurable. Create a module like this:
from pyramid.paster import get_app
config = '/path/to/app/production.ini'
myApp = get_app(config, 'main')
Then run twistd
like this:
$ twistd web --port tcp:8888 --wsgi foo.myApp
Where foo
is the name of the module you create.
Upvotes: 3
Reputation: 1072
I found a working solution. Here is the working .tac file:
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() gets the Pyramid WSGI application, while internet.TCPServer needs a Twisted Application object, so these should not be confused.
This code will start the app on TCP port 8888.
If someone has a better/clearer way to implement this, please add your answer.
Upvotes: 2