简体   繁体   English

django如何通过全局变量并发处理并发请求?

[英]How django handles simultaneous requests with concurrency over global variables?

I have a django instance hosted via apache/mod_wsgi. 我有一个通过apache / mod_wsgi托管的django实例。 I use pre_save and post_save signals to store the values before and after save for later comparisons. 我使用pre_savepost_save信号来存储保存前后的值,以便以后进行比较。 For that I use global variables to store the pre_save values which can be accessed in the post_save signal handler. 为此,我使用全局变量存储pre_save值,可以在post_save信号处理程序中访问该值。

My question is, if two requests A and B come together simultaneously requesting a same web service, will it be concurrent? 我的问题是,如果两个请求A和B同时在一起请求同一Web服务,它将是并发的吗? The B should not read the global variable which is written by A and vice versa. B不应读取A编写的全局变量,反之亦然。

PS: I don't use any threading Lock on variables. PS:我不对变量使用任何线程Lock

This partly depends on your mod_wsgi configuration. 这部分取决于您的mod_wsgi配置。 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. 在多线程配置中,不能保证使用全局变量不会混淆请求。

You should be able to find some more local place to stash the data you need between pre_save and post_save . 您应该能够找到更多本地位置,以在pre_savepost_save之间存储所需的数据。 I'd recommend putting some more thought into your design. 我建议您在设计中多加思考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM