3

I'm trying to run multiple process in Tornado and I tried the suggestions made on this thread : run multiple tornado processess

But the error hasn't gone for me. This is the server file.

server.py

import os
import sys
import tornado
#import pymongo

from tornado import ioloop, web, httpserver, websocket
from tornado.options import options

#Loading default setting files
import settings


#Motorengine - ODM for mongodb
#from motorengine import connect

app = tornado.web.Application(handlers=[
  (r'/', MainHandler),
  (r'/ws', WSHandler),
  (r'/public/(.*)', tornado.web.StaticFileHandler, {'path': options.public_path})],
  template_path=os.path.join(os.path.dirname(__file__), "app/templates"),
  static_path= options.static_path,
  autoreload=True,
  #images=os.path.join(os.path.dirname(__file__), "images"),
  debug=False)

if __name__ == '__main__':
   #read settings from commandline
    options.parse_command_line()
    server = tornado.httpserver.HTTPServer(app, max_buffer_size=1024*1024*201)
    server.bind(options.port)
    # autodetect cpu cores and fork one process per core
    server.start(0)

    #app.listen(options.port,xheaders=True)
    try:
        ioloop = tornado.ioloop.IOLoop.instance()
        #connect("attmlplatform", host="localhost", port=27017, io_loop=ioloop)
        print("Connected to database..")
        ioloop.start()
        print ('Server running on http://localhost:{}'.format(options.port))

    except KeyboardInterrupt:
        tornado.ioloop.IOLoop.instance().stop()

I've commented out the 'connect' import based on the anticipation that it may be triggering the instance and I'm not connecting to the database at all. This is just trying to get the server up.

This is the entire trace :

  File "server.py", line 52, in <module>
    server.start(0)
  File "/home/vagrant/anaconda3/envs/py34/lib/python3.4/site-packages/tornado/tcpserver.py", line 200, in start
    process.fork_processes(num_processes)
  File "/home/vagrant/anaconda3/envs/py34/lib/python3.4/site-packages/tornado/process.py", line 126, in fork_processes
    raise RuntimeError("Cannot run in multiple processes: IOLoop instance "
RuntimeError: Cannot run in multiple processes: IOLoop instance has already been initialized. You cannot call IOLoop.instance() before calling start_processes()

Any suggestions much appreciated! Thanks!

Community
  • 1
  • 1
vivekanon
  • 1,813
  • 3
  • 22
  • 44

1 Answers1

7

autoreload is incompatible with multi-process mode. When autoreload is enabled you must run only one process.

Ben Darnell
  • 21,844
  • 3
  • 29
  • 50
  • Then there's something that's not present in this code sample that is initializing the IOLoop (perhaps in the imports where your handlers are defined). This can be tricky to track down; rather than trying to make multi-process mode work it may be better to just switch to an external process manager like supervisord (which is IMHO a better solution in the long run, although it does require some initial setup work). – Ben Darnell Sep 11 '15 at 18:13
  • 3
    Setting 'debug=True' will enables this setting (autoreload=True) too . – SimonShyu Jun 01 '16 at 01:22
  • yep, `debug=True` was the solution for me – Ricky Levi Jun 02 '19 at 12:56