• 补充


    pipreqs:

    - 项目依赖 pip3 install pipreqs
    - 生成依赖文件:pipreqs ./
    - 安装依赖文件:pip3 install -r requirements.txt

     

    函数和方法

    from types import MethodType,FunctionType

    class Foo(object):
    def fetch(self):
    pass

    print(isinstance(Foo.fetch,MethodType))
    print(isinstance(Foo.fetch,FunctionType)) # True

    obj = Foo()
    print(isinstance(obj.fetch,MethodType)) # True
    print(isinstance(obj.fetch,FunctionType))

    threading.local

    多个线程修改同一个数据,复制多份变量给每个线程用,为每个线程开辟一块空间进行数据存储

    不用threading.local

    # 不用local
    from threading import Thread
    import time
    lqz = -1
    def task(arg):
       global lqz
       lqz = arg
       # time.sleep(2)
       print(lqz)

    for i in range(10):
       t = Thread(target=task,args=(i,))
       t.start()

    threading.local使用

    from threading import Thread
    from threading import local
    import time
    from threading import get_ident
    # 特殊的对象
    lqz = local()
    def task(arg):
       # 对象.val = 1/2/3/4/5
       lqz.value = arg
       time.sleep(2)
       print(lqz.value)
    for i in range(10):
       t = Thread(target=task,args=(i,))
       t.start()

    通过字典自定义threading.local(函数)

    from threading import get_ident,Thread
    import time
    storage = {}
    def set(k,v):
       ident = get_ident()
       if ident in storage:
           storage[ident][k] = v
       else:
           storage[ident] = {k:v}
    def get(k):
       ident = get_ident()
       return storage[ident][k]
    def task(arg):
       set('val',arg)
       v = get('val')
       print(v)

    for i in range(10):
       t = Thread(target=task,args=(i,))
       t.start()

    面向对象版

    from threading import get_ident,Thread
    import time
    class Local(object):
       storage = {}
       def set(self, k, v):
           ident = get_ident()
           if ident in Local.storage:
               Local.storage[ident][k] = v
           else:
               Local.storage[ident] = {k: v}
       def get(self, k):
           ident = get_ident()
           return Local.storage[ident][k]
    obj = Local()
    def task(arg):
       obj.set('val',arg)
       v = obj.get('val')
       print(v)
    for i in range(10):
       t = Thread(target=task,args=(i,))
       t.start()

    通过setattr和getattr实现

    from threading import get_ident,Thread
    import time
    class Local(object):
       storage = {}
       def __setattr__(self, k, v):
           ident = get_ident()
           if ident in Local.storage:
               Local.storage[ident][k] = v
           else:
               Local.storage[ident] = {k: v}
       def __getattr__(self, k):
           ident = get_ident()
           return Local.storage[ident][k]
    obj = Local()
    def task(arg):
       obj.val = arg
       print(obj.val)
    for i in range(10):
       t = Thread(target=task,args=(i,))
       t.start()

    每个对象有自己的存储空间(字典)

    from threading import get_ident,Thread
    import time
    class Local(object):
       def __init__(self):
           object.__setattr__(self,'storage',{})
       def __setattr__(self, k, v):
           ident = get_ident()
           if ident in self.storage:
               self.storage[ident][k] = v
           else:
               self.storage[ident] = {k: v}
       def __getattr__(self, k):
           ident = get_ident()
           return self.storage[ident][k]
    obj = Local()
    def task(arg):
       obj.val = arg
       obj.xxx = arg
       print(obj.val)
    for i in range(10):
       t = Thread(target=task,args=(i,))
       t.start()

    兼容线程和协程

    try:
       from greenlet import getcurrent as get_ident
    except Exception as e:
       from threading import get_ident
    from threading import Thread
    import time
    class Local(object):
       def __init__(self):
           object.__setattr__(self,'storage',{})
       def __setattr__(self, k, v):
           ident = get_ident()
           if ident in self.storage:
               self.storage[ident][k] = v
           else:
               self.storage[ident] = {k: v}
       def __getattr__(self, k):
           ident = get_ident()
           return self.storage[ident][k]
    obj = Local()
    def task(arg):
       obj.val = arg
       obj.xxx = arg
       print(obj.val)
    for i in range(10):
       t = Thread(target=task,args=(i,))
       t.start()

    partial偏函数

    #偏函数的第二个部分(可变参数),按原有函数的参数顺序进行补充,参数将作用在原函数上,最后偏函数返回一个新函数
    from functools import partial
    def test(a,b,c,d):
       return a+b+c+d

    tes=partial(test,1,2)
    print(tes(3,4))

    session源码分析

    SecureCookieSessionInterface
    -open_session
    -save_session

     

     

  • 相关阅读:
    大同世界的Java 和.NET 开发
    关于Memcached 你了解多少?
    C#二进制与字符串之间的相互转换
    Nopcommerce主要用到的技术及特点
    小程序之路
    .NET方面的框架的整理和总结
    如何将FastReportOnlineDesign 灵活的应用到C/S B/S 程序当中?
    WebApi 的CRUD 的方法的应用
    关于EF 通用增删改查的封装
    基于Json序列化和反序列化通用的封装
  • 原文地址:https://www.cnblogs.com/xuechengeng/p/10821356.html
Copyright © 2020-2023  润新知