Where to setup Python environment attributes for a Django project?

时间:2021-10-15 23:22:41

For example the Python decimal.Decimal() class has a context. You can view the current context with getcontext() and set new values for precision, rounding, or enable traps.

例如,Python decimal.Decimal()类有一个上下文。您可以使用getcontext()查看当前上下文,并为精度,舍入或启用陷阱设置新值。

If you wanted to set a new value for the context so this is visible throughout a Django project, where would be best to do so?

如果你想为上下文设置一个新值,那么这在整个Django项目中是可见的,哪里最好这样做?

e.g. Throughout the project the FloatOperation signal should be trapped.

例如在整个项目中,应该捕获FloatOperation信号。

from decimal import FloatOperation, getcontext

context = getcontext()
context.traps[FloatOperation] = True

Also using getcontext() return the current context for the active thread. Aside from explicitly creating new threads in a project, are there any additional consideration to make with Django creating additional threads.

还使用getcontext()返回活动线程的当前上下文。除了在项目中显式创建新线程之外,还有任何额外的考虑要使用Django创建额外的线程。

1 个解决方案

#1


2  

I'd suggest an application core, or common, that includes your setup as part of AppConfig.ready().

我建议一个应用程序核心,或者常见的,包括你的设置作为AppConfig.ready()的一部分。

https://docs.djangoproject.com/en/1.7/ref/applications/#django.apps.AppConfig.ready

#1


2  

I'd suggest an application core, or common, that includes your setup as part of AppConfig.ready().

我建议一个应用程序核心,或者常见的,包括你的设置作为AppConfig.ready()的一部分。

https://docs.djangoproject.com/en/1.7/ref/applications/#django.apps.AppConfig.ready