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

后端 未结 2 1316
栀梦
栀梦 2021-02-04 07:25

To achieve something similar to google app engines \'deferred calls\' (i.e., the request is handled, and afterwards the deferred task is handled), i experimented a little and ca

相关标签:
2条回答
  • 2021-02-04 07:56

    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.

    0 讨论(0)
  • 2021-02-04 08:12

    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.

    0 讨论(0)
提交回复
热议问题