• 补充


    1 pipreqs:

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

    按照上面的方法可以生成项目中所有安装的模块及版本号

    2 函数和方法

    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))
    class Foo:
        def test(self):
            return 'hello'
    
    obj=Foo()
    obj.test() #这个test叫做方法
    
    Foo.test() #这个叫做函数

    一般情况下用函数方法定义的就是函数,用对象调用的就是方法,在类中调用的还是函数

    3 threading.loca

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

    不用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偏函数

    #偏函数的第二个部分(可变参数),按原有函数的参数顺序进行补充,参数将作用在原函数上,最后偏函数返回一个新函数
  • 相关阅读:
    Redis 安装(Windows)
    etcd简介与应用场景
    Nginx+SignalR+Redis(二)windows
    Nginx+SignalR+Redis(一)windows
    Windows 版MongoDB 复制集Replica Set 配置
    走进异步世界async、await
    认识和使用Task
    进程、应用程序域、线程的相互关系
    ASP.NET Core实现类库项目读取配置文件
    用idea做springboot开发,设置thymeleaf时候,新手容易忽略误区
  • 原文地址:https://www.cnblogs.com/ouyang99-/p/10397233.html
Copyright © 2020-2023  润新知