在wsgi应用程序中生成线程可以吗? [英] Is it ok to spawn threads in a wsgi-application?

查看:95
本文介绍了在wsgi应用程序中生成线程可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要实现类似于Google应用引擎的延迟调用(即,请求被处理,然后处理延迟任务),我尝试了一点,并提出了解决方案来产生一个线程,其中我的延期呼叫被处理。

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 came up with the solution to spawn a thread in which my deferred call is handled.

我现在正在尝试确定这是否可以接受。

I am now trying to determine if this is an acceptable way.

是否可能(根据WSGI规范),在处理实际请求之后,但在所有线程耗尽之前,该进程由Web服务器终止?

Is it possible (according to the WSGI specification) that the process is terminated by the webserver after the actual request is handled, but before all threads run out?

(如果有更好的方法,这也会很好)

(if there's a better way, that would be also fine)

推荐答案

FWIW,还有一个阅读:

FWIW, also have a read of:

http://code.google.com/p/modwsgi/wiki/注册CleanupCode

在ActionScript中,只有在WSGI规范本身的上下文中执行延迟工作才是唯一的方法来关闭()iterable。虽然这不是在单独的线程中,并且将发生在实际请求的上下文中,尽管响应应该已被刷新回客户端。因此,您的延迟操作将消耗该请求线程,直到工作完成,因此请求线程在此之前无法处理其他请求。

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.

一般来说,如果您使用后台线程,不能保证任何主机机制将等到这些后台线程在关闭进程之前完成。实际上,甚至不能想到任何标准的部署机制,等等。甚至没有真正的保证,在过程关闭时将会调用atexit处理程序,引用的文档也会简要介绍一下。

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.

这篇关于在wsgi应用程序中生成线程可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆