3

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?

5 Answers 5

4

I found what I was looking for:

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

you can register several ports when creating a handler

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

application.listen(8080)
application.listen(8081)
1
  • 1
    This should be the accepted answer, as the whole point of a Tornado server is self-encapsulation. It should be able to listen on multiple ports and dispatch multiple web.Applications.
    – cowbert
    Commented Feb 6, 2018 at 18:57
3

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.

2

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

2
  • adding a runtime option that configures the port is all that is necessary. there is also now a commit on github that lets tornado fork off one process per core in your machine.
    – Carson
    Commented Dec 23, 2009 at 0:24
  • No. just use --port=<different port> Commented Sep 9, 2012 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.