About decorators:
In terms of function, similar to filters and interceptors, add some custom logic before and after the execution of the original function.
In terms of implementation, the original function is passed in the decorator parameters, and the decorator controls the execution of the original function.
In terms of usage, add the @ decorator function name to the original function header like a java annotation.
Decorator writing:
(1) Thread Mode_Change: It is a three-layer structure in which the decorator contains additional input parameters; when using it, there must be (), @thread mode_改()
(2) Thread mode: the decorator does not require additional input parameters, two-layer structure; there is no () when used, @thread mode
import threading
from concurrent import futures
from functools import wraps
# region 线程
# -- 关于初始化区,扫描到几个@就执行几次
__线程池_装饰专用 = futures.ThreadPoolExecutor(12)
def 线程模式_改(is_VIP = False, VIP_name = None): # 这里的参数,是给装饰器的参数
# region 装饰器的初始化区1
# endregion
def wrap(func):
# region 装饰器的初始化区3
# endregion
@wraps(func) # 复制原始函数信息,并保留下来
def inner(*args, **kwargs): # args和kwargs,是原始函数的参数;args是元祖,kwargs是字典
# region 执行原始函数前
# endregion
if is_VIP:
rst = threading.Thread(target=func, name=VIP_name, args=args, kwargs=kwargs)
rst.start()
else:
rst = __线程池_装饰专用.submit(func, *args, **kwargs) # 执行原始函数
# region 执行原始函数后
# endregion
return rst
# region 装饰器的初始化区4
# endregion
return inner
# region 装饰器的初始化区2
# endregion
return wrap
def 线程模式(func):
# region 装饰器的初始化区3
# endregion
@wraps(func) # 复制原始函数信息,并保留下来
def inner(*args, **kwargs): # args和kwargs,是原始函数的参数;args是元祖,kwargs是字典
# region 执行原始函数前
# endregion
rst = __线程池_装饰专用.submit(func, *args, **kwargs) # 执行原始函数
# region 执行原始函数后
# endregion
return rst
# region 装饰器的初始化区4
# endregion
return inner
# endregion
Example:
@线程模式
@打点计时
def a1(s = None):
for i in range(5):
time.sleep(1)
print_加锁(s)
for i in range(10):
a1(i)
Thread decorator, timing decorator
import threading
from concurrent import futures
from functools import wraps
__lock_print = threading.Lock()
def print_加锁(*args, **kwargs):
with __lock_print:
print(*args, **kwargs)
# region 计时
def 打点计时(func):
@wraps(func) # 复制原始函数信息,并保留下来
def inner(*args, **kwargs): # args和kwargs,是原始函数的参数;args是元祖,kwargs是字典
# region 执行原始函数前
计时器 = 打点计时类.实例化()
计时器.打点()
# endregion
rst = func(*args, **kwargs) # 执行原始函数
# region 执行原始函数后
计时器.打点()
print_加锁(f'''{func.__name__}: {计时器.计时()}''')
# endregion
return rst
return inner
# endregion
# region 线程
__线程池_装饰专用 = futures.ThreadPoolExecutor(12)
def 线程模式_改(is_VIP = False, VIP_name = None): # 这里的参数,是给装饰器的参数
# region 装饰器的初始化区1
# endregion
def wrap(func):
# region 装饰器的初始化区3
# endregion
@wraps(func) # 复制原始函数信息,并保留下来
def inner(*args, **kwargs): # args和kwargs,是原始函数的参数;args是元祖,kwargs是字典
# region 执行原始函数前
# endregion
if is_VIP:
rst = threading.Thread(target=func, name=VIP_name, args=args, kwargs=kwargs)
rst.start()
else:
rst = __线程池_装饰专用.submit(func, *args, **kwargs) # 执行原始函数
# region 执行原始函数后
# endregion
return rst
# region 装饰器的初始化区4
# endregion
return inner
# region 装饰器的初始化区2
# endregion
return wrap
def 线程模式(func):
# region 装饰器的初始化区3
# endregion
@wraps(func) # 复制原始函数信息,并保留下来
def inner(*args, **kwargs): # args和kwargs,是原始函数的参数;args是元祖,kwargs是字典
# region 执行原始函数前
# endregion
rst = __线程池_装饰专用.submit(func, *args, **kwargs) # 执行原始函数
# region 执行原始函数后
# endregion
return rst
# region 装饰器的初始化区4
# endregion
return inner
# endregion
Dotting and timing category: https://blog.csdn.net/u013595395/article/details/108763819