I've got a Twisted Klein app which needs to listen on more than one port in our deployment environment. With Flask I'd deploy with gunicorn and it seems like you could run Klein in a wsgi container but it'd defeat the async nature of it.
Asked
Active
Viewed 598 times
1 Answers
0
Inspired by this question about Twisted listening on multiple ports I dug into the Klein app.run() method and it seems to work if you just set up another Twisted endpoint before you call app run. Any insight into whether this is the right approach would be appreciated.
from twisted.web.server import Site
from twisted.internet import reactor, endpoints
...
admin_endpoint = "tcp:port={0}:interface={1}".format(8888, '0.0.0.0')
endpoints.serverFromString(reactor, admin_endpoint).listen(Site(app.resource()))
...
app.run('0.0.0.0', port=9999)

Community
- 1
- 1

patricksurry
- 5,508
- 2
- 27
- 38
-
If designed with care and precision, a single ``klein`` app can scale equally well as a gunicron process running 3 or 4 ``flask`` apps. Your example looks good but keep in mind that this will be a single threaded application. Gunicorn forks wsgi objects into multiple individual process, which allows you to scale. One way to get similar results would be to use a load balancer (``nginx`` has one built in) in combination with multiple ``klien`` processes on differnt ports. Is there a particular problem that's making you lean towards gunicron? – notorious.no Dec 02 '16 at 04:54
-
Right, I would still scale by having multiple copies of the klein process running, each with their own pair of ports. – patricksurry Dec 03 '16 at 20:43