Django是如何处理并发了全局变量的并发请求? [英] How django handles simultaneous requests with concurrency over global variables?
问题描述
我通过Apache / mod_wsgi的托管Django的实例。我使用 pre_save
和 post_save
信号前值存储,并保存为以后的比较之后。对于我使用全局变量来存储可以在 post_save
信号处理程序要访问的 pre_save
值。
I have a django instance hosted via apache/mod_wsgi. I use pre_save
and post_save
signals to store the values before and after save for later comparisons. For that I use global variables to store the pre_save
values which can be accessed in the post_save
signal handler.
我的问题是,如果两个请求A和B同时走到一起请求相同的Web服务,会是并发?而B不应该读这是由反之亦然写的全局变量。
My question is, if two requests A and B come together simultaneously requesting a same web service, will it be concurrent? The B should not read the global variable which is written by A and vice versa.
PS:我不使用任何线程锁定
的变量
PS: I don't use any threading Lock
on variables.
推荐答案
这部分取决于你的的mod_wsgi
配置。如果您配置为使用每个进程只有一个线程,那么全局变量是安全的 - 尽管我不推荐使用它们,因为各种各样的原因。在多线程配置,没有什么低保请求将不会弄混,如果你使用全局变量。
This partly depends on your mod_wsgi
configuration. If you configure it to use only one thread per process, then global variables are safe--although I wouldn't recommend using them, for a variety of reasons. In a multi-thread configuration, there is nothing guaranteeing that requests won't get mixed up if you use global variables.
您应该能够找到一些地方的地方藏匿您需要 pre_save
和 post_save $ C $之间的数据C>。我建议你把一些更多的思考到您的设计。
You should be able to find some more local place to stash the data you need between pre_save
and post_save
. I'd recommend putting some more thought into your design.
这篇关于Django是如何处理并发了全局变量的并发请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!