我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用werkzeug.local.Local()。
def release_local(local): """Releases the contents of the local for the current context. This makes it possible to use locals without a manager. Example:: >>> loc = Local() >>> loc.foo = 42 >>> release_local(loc) >>> hasattr(loc, 'foo') False With this function one can release :class:`Local` objects as well as :class:`LocalStack` objects. However it is not possible to release data held by proxies that way, one always has to retain a reference to the underlying local object in order to be able to release it. .. versionadded:: 0.6.1 """ local.__release_local__()
def test_basic_local(self): l = local.Local() l.foo = 0 values = [] def value_setter(idx): time.sleep(0.01 * idx) l.foo = idx time.sleep(0.02) values.append(l.foo) threads = [Thread(target=value_setter, args=(x,)) for x in [1, 2, 3]] for thread in threads: thread.start() time.sleep(0.2) assert sorted(values) == [1, 2, 3] def delfoo(): del l.foo delfoo() self.assert_raises(AttributeError, lambda: l.foo) self.assert_raises(AttributeError, delfoo) local.release_local(l)
def test_custom_idents(self): ident = 0 loc = local.Local() stack = local.LocalStack() mgr = local.LocalManager([loc, stack], ident_func=lambda: ident) loc.foo = 42 stack.push({'foo': 42}) ident = 1 loc.foo = 23 stack.push({'foo': 23}) ident = 0 assert loc.foo == 42 assert stack.top['foo'] == 42 stack.pop() assert stack.top is None ident = 1 assert loc.foo == 23 assert stack.top['foo'] == 23 stack.pop() assert stack.top is None