Is it ok to spawn threads in a wsgi-application? Is it ok to spawn threads in a wsgi-application? flask flask

Is it ok to spawn threads in a wsgi-application?


WSGI does not specify the lifetime of an application process (as WSGI application is a Python callable object). You can run it in a way that is completely independent of the web server, in which case, only you control the lifetime.

There is also nothing in the WSGI that would prohibit you from spawning threads, or processes, or doing whatever the hell you want.


FWIW, also have a read of:

http://code.google.com/p/modwsgi/wiki/RegisteringCleanupCode

The hooking of actions to close() of iterable is the only way within context of the WSGI specification itself for doing deferred work. That isn't in a separate thread though and would occur within the context of the actual request, albeit after the response is supposed to have been flushed back to the client. Thus your deferred action will consume that request thread until the work is complete and so that request thread would not be able to handle other requests until then.

In general, if you do use background threads, there is no guarantee that any hosting mechanism would wait until those background threads complete before shutting process down. In fact, can't even think of any standard deployment mechanism which does wait. There isn't really even a guarantee that atexit handlers will be called on process shutdown, something that the referenced documentation also briefly talks about.