- 项目依赖 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() #这个叫做函数
一般情况下用函数方法定义的就是函数,用对象调用的就是方法,在类中调用的还是函数
不用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()
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()
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()
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()
#偏函数的第二个部分(可变参数),按原有函数的参数顺序进行补充,参数将作用在原函数上,最后偏函数返回一个新函数