I am using Tornado web framework but one of my deployment environments is mod_wsgi on prefork Apache 2 (weird hosting requirement).
Is processes=n threads=1
the correct way to set up my WSGIDaemonProcess
for a tornado.wsgi.WSGIAdapter
? My own code should be threadsafe however, I am binding a SQLAlchemy Session() to an instance of tornado.web.RequestHandler
in the handler's .prepare()
as here (I'm obviously not using coroutines in WSGI-mode). I'm also using blocking tornado.httpclient.HTTPClient
in some request handlers.
Also, does WSGIDaemonProcess
process fork() comparably to tornado.httpserver.HTTPServer().start(n)
?
(I am posting this question here since I have no idea how to ask both @Ben Darnell and @Graham Dumpleton at the same time without spamming both tornado and mod_wsgi lists).
WSGIAdapter
is thread-safe, and so is the subset of Tornado that works with it (RequestHandler.write
, for example, is thread-safe when used with WSGIAdapter
, but not when used with IOLoop
). So when running with WSGIAdapter
, you can configure your WSGI server in the same way you would for any other WSGI framework (typically something like 1 process per CPU core (this is comparable to what you get with HTTPServer.start(n)
) and 5-10 threads per process depending on how much of your time you spend in I/O).
Note that tornado.httpclient.HTTPClient
objects are not thread-safe, so you'll need to put a lock around it or use a separate object per thread.