Threads and local proxy in Werkzeug. Usage

江枫思渺然 提交于 2019-12-10 13:20:30

问题


At first I want to make sure that I understand assignment of the feature correct. The local proxy functionality assigned to share a variables (objects) through modules (packages) within a thread. Am I right?

At second, the usage is still unclear for me, maybe because I misunderstood an assignment. I use Flask. If I have two (or more) modules: A, B. I want to import object C from module A to module B. But I can't do it in the usual way, from A import C, because it will cause a looped import and thereafter ImportError. How to solve this issue with Werkzeug Local Proxy? And should I do it with Werkzeug?

module A:

from werkzeug.local import LocalSomething # LocalProxy or LocalStack

C = 'C'
# Somehow add C to LocalSomething

module B:

from werkzeug.locla import LocalSomething

C = LocalSomething()['C']

回答1:


Module Z:

from werkzeug.local import Local
myLocals = Local()

module A:

from Z import myLocals
myLocals.C = "C"

module B:

from Z import myLocals
C = getattr(myLocals, "C", None)

is this you're looking for?



来源:https://stackoverflow.com/questions/14934885/threads-and-local-proxy-in-werkzeug-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!