Search code examples
pythonimportflaskwerkzeug

Threads and local proxy in Werkzeug. Usage


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']

Solution

  • 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?