flask上下文管理相关-LocalStack 对象维护栈
LocalStack 对象维护栈
模拟
import threading
"""
storage = {
1232: {stack:[123,456]}
}
"""
class Local(object):
def __init__(self):
object.__setattr__(self, 'storage', {})
def __setattr__(self, key, value):
ident = threading.get_ident() # 1233
if ident in self.storage:
self.storage[ident][key] = value
else:
self.storage[ident] = {key: value}
def __getattr__(self, item):
ident = threading.get_ident()
if ident not in self.storage:
return
if item not in self.storage[ident]:
return
return self.storage[ident][item]
class LocalStack(object):
def __init__(self):
self._local = Local()
def push(self, value):
"""
将值放入栈
:param value:
:return:
"""
re = self._local.stack
if re:
self._local.stack.append(value)
else:
self._local.stack = [value,]
return re
def pop(self,):
"""
从栈中拿走值
:return:
"""
if not self._local.stack:
return
if len(self._local.stack) == 1:
data = self._local.stack.pop()
# 当stack中的元素为1时,执行pop操作后,要销毁stack,这里暂时没有做
return data
else:
return self._local.stack.pop()
def top(self):
"""
查看栈顶的数据
:return:
"""
if not self._local.stack:
return
return self._local.stack[-1]
st = LocalStack()
st.push(123)
st.push(456)
a = st.top()
b = st.pop()
b = st.pop()
# # b = st.pop()
a = st.top()
print(b,a)
源码展示
LocalStack部分
class LocalStack(object):
"""This class works similar to a :class:`Local` but keeps a stack
of objects instead. This is best explained with an example::
>>> ls = LocalStack()
>>> ls.push(42)
>>> ls.top
42
>>> ls.push(23)
>>> ls.top
23
>>> ls.pop()
23
>>> ls.top
42
They can be force released by using a :class:`LocalManager` or with
the :func:`release_local` function but the correct way is to pop the
item from the stack after using. When the stack is empty it will
no longer be bound to the current context (and as such released).
By calling the stack without arguments it returns a proxy that resolves to
the topmost item on the stack.
.. versionadded:: 0.6.1
"""
def __init__(self):
self._local = Local()
def __release_local__(self):
self._local.__release_local__()
def _get__ident_func__(self):
return self._local.__ident_func__
def _set__ident_func__(self, value):
object.__setattr__(self._local, "__ident_func__", value)
__ident_func__ = property(_get__ident_func__, _set__ident_func__)
del _get__ident_func__, _set__ident_func__
def __call__(self):
def _lookup():
rv = self.top
if rv is None:
raise RuntimeError("object unbound")
return rv
return LocalProxy(_lookup)
def push(self, obj):
"""Pushes a new item to the stack"""
rv = getattr(self._local, "stack", None)
if rv is None:
self._local.stack = rv = []
rv.append(obj)
return rv
def pop(self):
"""Removes the topmost item from the stack, will return the
old value or `None` if the stack was already empty.
"""
stack = getattr(self._local, "stack", None)
if stack is None:
return None
elif len(stack) == 1:
release_local(self._local)
return stack[-1]
else:
return stack.pop()
@property
def top(self):
"""The topmost item on the stack. If the stack is empty,
`None` is returned.
"""
try:
return self._local.stack[-1]
except (AttributeError, IndexError):
return None
flask上下文管理相关-LocalStack 对象维护栈的更多相关文章
- flask上下文管理相关 - threading.local 以及原理剖析
threading.local 面向对象相关: setattr/getattr class Foo(object): pass obj = Foo() obj.x1 = 123 # object.__ ...
- Flask上下文管理、session原理和全局g对象
一.一些python的知识 1.偏函数 def add(x, y, z): print(x + y + z) # 原本的写法:x,y,z可以传任意数字 add(1,2,3) # 如果我要实现一个功能, ...
- Flask上下文管理机制
前引 在了解flask上下文管理机制之前,先来一波必知必会的知识点. 面向对象双下方法 首先,先来聊一聊面向对象中的一些特殊的双下划线方法,比如__call__.__getattr__系列.__get ...
- Flask上下文管理
一.一些python的知识 1.偏函数 def add(x, y, z): print(x + y + z) # 原本的写法:x,y,z可以传任意数字 add(1,2,3) # 如果我要实现一个功能, ...
- Flask 上下文管理
为什么用threading.local? 我们都知道线程是由进程创建出来的,CPU实际执行的也是线程,那么线程其实是没有自己独有的内存空间的,所有的线程共享进程的资源和空间,共享就会有冲突,对于多线程 ...
- Flask 上下文管理-- (session,request,current_app的传递)--类似本地线程实现,以及多app应用
Flask session,request,current_app的传递 请求上下文的作用 -- 封装请求相关得数据(request,session) 请求上下文 request session re ...
- Flask - 上下文管理(核心)
参考 http://flask.pocoo.org/docs/1.0/advanced_foreword/#thread-locals-in-flask https://zhuanlan.zhihu. ...
- Flask上下文管理及源码刨析
基本流程概述 - 与django相比是两种不同的实现方式. - django/tornado是通过传参数形式实现 - 而flask是通过上下文管理, 两种都可以实现,只不实现的方式不一样罢了. - 上 ...
- flask 上下文管理 &源码剖析
基本流程概述 - 与django相比是两种不同的实现方式. - django/tornado是通过传参数形式实现 - 而flask是通过上下文管理, 两种都可以实现,只不实现的方式不一样罢了. - 上 ...
随机推荐
- Android Studio +git+jenkins+gitHub
之前做的jenkins+gitlab的jmeter,,,其实已经记不太清楚了. 最近又在搞Android学习,就试一下AndroidStudio+jenkins自动打包apk看看. step1: An ...
- xss之上传文件的xss,绕过csp,预警机制
xss1.XSS姿势——文件上传XSS https://wooyun.x10sec.org/static/drops/tips-14915.html总结: 1.1.文件名方式,原理:有些文件名可能反应 ...
- 算法:统计1-n中,1出现的次数
package edu.cqu.algorithmTest; import java.util.Scanner; /* * 给定一个十进制正整数N,写下从1开始,到N的所有整数,然后数一下其中出现的所 ...
- python3爬虫系列19之反爬随机 User-Agent 和 ip代理池的使用
站长资讯平台:python3爬虫系列19之随机User-Agent 和ip代理池的使用我们前面几篇讲了爬虫增速多进程,进程池的用法之类的,爬虫速度加快呢,也会带来一些坏事. 1. 前言比如随着我们爬虫 ...
- python3.6 创建字典三法
这里献丑给出 python 3.6 创建字典变量的三法 其一:阳春白雪法:直接声明 mydic = {"name":"徐晓冬","age" ...
- spring+mybatis所需各种服务的搭建
1.mysql5.6 2.jdk1.7 3.zookeeper 4.dubbo-admin 5.activtymq 6.maven 7.nexus 8.mybatis脚本文件生成工具使用
- 6、Spring Boot 2.x 集成 MyBatis
1.6 Spring Boot 2.x 集成 MyBatis 简介 详细介绍如何在Spring Boot中整合MyBatis,并通过注解方式实现映射. 完整源码: 1.6.1 创建 spring-bo ...
- 深入理解Java输入输出流
Java.io包的File类,File类用于目录和文件的创建.删除.遍历等操作,但不能用于文件的读写. Java 对文件的写入和读取涉及到流的概念,写入为输出流,读取为输入流.如何理解流的概念呢?可以 ...
- SQL Server遇到的错误和有用的tools
1.The target principal name is incorrect. Cannot generate SSPI context. 检查IIS的profile,可能是密码错误 2.The ...
- [DK] 化学竞赛的大奖
https://www.luogu.org/problemnew/show/T16502 无向图 缩点 树的直径 到直径两个端点的距离的较大值 #include <iostream> ...