zoukankan      html  css  js  c++  java
  • 补充

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

    #偏函数的第二个部分(可变参数),按原有函数的参数顺序进行补充,参数将作用在原函数上,最后偏函数返回一个新函数
  • 相关阅读:
    eclipse 关闭web项目无用校验
    Java7的那些新特性
    Linux内核源码情景分析-wait()、schedule()
    android canvas 画图笔记
    android启动第一个界面时即闪屏的核心代码(两种方式)
    leetCode(24):Binary Search Tree Iterator
    12:打印 1 到最大的 n 位数
    Android仿QQ ios dialog,仿QQ退出向上菜单
    iOS UI16_数据持久化
    Android自己定义百度地图缩放图标
  • 原文地址:https://www.cnblogs.com/ouyang99-/p/10397233.html
Copyright © 2011-2022 走看看