Django初始化 [英] Django Initialization

查看:43
本文介绍了Django初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大数组,当django启动时,我只想将其加载到内存中一次,然后将其视为只读全局变量.放置代码以初始化该数组的最佳位置是什么?

I have a big array, that I would like to load into memory only once when django starts up and then treat it as a read only global variable. What is the best place to put the code for the initialization of that array?

如果我将其放在settings.py中,则每次导入设置模块时都会重新初始化它,对吗?

If I put it in settings.py it will be reinitialized every time the settings module is imported, correct?

推荐答案

settings.py是正确的选择.像任何其他模块一样,Settings.py被加载一次.仍然存在一个问题,即每个进程必须一次导入一个模块,因此Web服务器的重生样式(如apache)将为每个相关实例重新加载一次.对于mod_python,每个进程一次.对于mod_wsgi,除非您必须重新启动,否则这可能只是一次.

settings.py is the right place for that. Settings.py is, like any other module, loaded once. There is still the problem of the fact that a module must be imported once for each process, so a respawning style of web server (like apache) will reload it once for each instance in question. For mod_python this will be once per process. for mod_wsgi, this is likely to be just one time, unless you have to restart.

tl; dr模块也将被导入一次.把它放到settings.py

tl;dr modules are imported once, even if multiple import statements are used. put it in settings.py

这篇关于Django初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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