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']
It could be that a request is reusing a thread from a previous request, and hence data is left over in the thread local object. Werkzeug provides its own implementation of local data storage called werkzeug. local . This approach provides a similar functionality to thread locals but also works with greenlets.
LocalProxy (local, name=None)[source] Acts as a proxy for a werkzeug local. Forwards all operations to a proxied object. The only operations not supported for forwarding are right handed operands and any kind of assignment.
Flask wraps Werkzeug, using it to handle the details of WSGI while providing more structure and patterns for defining powerful applications. Werkzeug includes: An interactive debugger that allows inspecting stack traces and source code in the browser with an interactive interpreter for any frame in the stack.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With