简体   繁体   中英

How django handles simultaneous requests with concurrency over global variables?

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.

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: I don't use any threading Lock on variables.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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