Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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']
like image 910
I159 Avatar asked Feb 18 '13 11:02

I159


People also ask

What is werkzeug local?

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.

What is LocalProxy in flask?

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.

Does flask use werkzeug?

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.


1 Answers

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?

like image 155
mderk Avatar answered Sep 24 '22 20:09

mderk