How can I start multiple Tornado Server instances in multiple ports

主宰稳场 提交于 2020-01-15 06:38:29

问题


I need to start the blog demo in the following ports:

127.0.0.1:8000 127.0.0.1:8001 127.0.0.1:8002 127.0.0.1:8003

When I run the application using:

./demos/blog/blog.py

it starts in port 8888 as defined by:

define("port", default=8888, help="run on the given port", type=int)

How do I run multiple instances in multiple ports?


回答1:


I found what I was looking for:

./demos/blog/blog.py --port=8889



回答2:


Make sure you know, the --port option gets parsed by the options module of the Tornado framework.

The lines that looks like this:

define("port", default=8888, help="Port to listen on", type=int)

and later there's a call to the options module that parses command line vars automatically.

I'm just giving you this because you might want to later specify different variables in your programs that you design around the framework that you may want to change instance to instance.




回答3:


Use supervisord to start multiple instances. Since each app takes the --port= argument you can set something like this up:

Here's the setup I use for Around The World

[group:aroundtheworld]
programs=aroundtheworld-10001,aroundtheworld-10002,aroundtheworld-10003

[program:aroundtheworld-10001]
command=/var/lib/tornado/aroundtheworld/app.py --port=10001
directory=/var/lib/tornado/aroundtheworld/
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/tornado/aroundtheworld-10001.log
stdout_logfile_maxbytes=500MB
stdout_logfile_backups=50
stdout_capture_maxbytes=1MB
stdout_events_enabled=false
loglevel=warn

[program:aroundtheworld-10002]
command=/var/lib/tornado/aroundtheworld/app.py --port=10002
directory=/var/lib/tornado/aroundtheworld/
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/tornado/aroundtheworld-10002.log
stdout_logfile_maxbytes=500MB
stdout_logfile_backups=50
stdout_capture_maxbytes=1MB
stdout_events_enabled=false
loglevel=warn

[program:aroundtheworld-10003]
command=/var/lib/tornado/aroundtheworld/app.py --port=10003
directory=/var/lib/tornado/aroundtheworld/
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/tornado/aroundtheworld-10003.log
stdout_logfile_maxbytes=500MB
stdout_logfile_backups=50
stdout_capture_maxbytes=1MB
stdout_events_enabled=false
loglevel=warn

If you need help with how to set up Nginx or something similar to load balance across these then submit a new question.




回答4:


you can register several ports when creating a handler

application = tornado.web.Application([
   (r".*", MainHandler),
], **app_settings)

application.listen(8080)
application.listen(8081)



回答5:


copy /demos/blog/blog.py to blog_otherports.py

change posts in blog_otherports.py

and python blog_otherports.py

you need to run two processes



来源:https://stackoverflow.com/questions/1447342/how-can-i-start-multiple-tornado-server-instances-in-multiple-ports

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