chris.george
chris.george

Reputation: 1

Tornado: Pre-forking with unix sockets

Using Tornado Web Server, I'm attempting to use their pre-fork after binding to a unix socket, but I get the following error:

RuntimeError: Cannot run in multiple processes: IOLoop instance has already been initialized. You cannot call IOLoop.instance() before calling start_processes()

Is there a reason tornado throws this issue when binding unix sockets and using:

myserver.start(0) vs using an TCP Port?

Upvotes: 0

Views: 663

Answers (2)

Atomicblah
Atomicblah

Reputation: 69

If you have debug=True in your application initialization you can run into this error. For example:

app = tornado.web.Application(handlers=[
.
.
],
debug=True)

As it automatically enabled autoreload=True which is not compatible with forking. So to work around this you need also set autoreload=False in the application initialization as well.

app = tornado.web.Application(handlers=[
.
.
],
debug=True, autoreload=False)

Upvotes: 0

Ben Darnell
Ben Darnell

Reputation: 22134

The error has nothing to do with unix sockets. IOLoops do not survive a fork gracefully, so if you are going to fork you must do it before initializing any global IOLoop (but after binding any sockets). In general, you must do as little as possible before the fork, since many Tornado components implicitly start the IOLoop. If you are using multiple TCPServers, be sure to only fork from the first one you start; all the others should be in single-process mode.

Upvotes: 1

Related Questions