加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
finderdeco.py 4.45 KB
一键复制 编辑 原始数据 按行查看 历史
老兵 提交于 2017-11-18 18:39 . revsie smaller out format
# 作者:Stansosleepy
# 链接:http://www.jianshu.com/p/ae5bc6093337
# 來源:简书
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
import os
# import sys
import functools
import threading
import inspect
import atexit
current_path = os.path.dirname(os.path.abspath(__file__))
log_path = os.path.join(current_path,"log")
logfile = os.path.join(log_path,"log.txt")
call_relations = []
# print("global-call_relations_len:%s" % len(call_relations))
def findcaller(func):
@functools.wraps(func)
def wrapper(*args,**kwargs):
# print("enter_func_name_-call_relations_len:%s" % len(call_relations))
# ---------------------------------------------------------------
infos = ""
frame=inspect.currentframe()
current_pid = os.getpid()
current_tname = threading.current_thread().name
while True:
try:
frame = frame.f_back
full_filename = frame.f_code.co_filename
filebasename = os.path.basename(full_filename)
filebasename = filebasename.replace(".","")
caller_func = frame.f_code.co_name
lineno = frame.f_lineno
inner_info = "%s.%s.%s" % (filebasename, caller_func, lineno)
if "wrapper" not in inner_info:
infos = "pid=%s.tid=%s.%s|%s" % (current_pid, current_tname, inner_info, infos)
except:
break
# --------写入------------------------------------------------------------------
temp01 = "%spid=%s.tid=%s.%spy.%s.\n" % (infos, current_pid, current_tname, func.__module__, func.__name__)
# temp02 = ("%spy.%s" % (func.__module__, func.__name__))
# temp03 = "%s|%s%s\n" % (temp01,infos,temp02)
may_replace = False
global call_relations
for i in range(len(call_relations)):
if call_relations[i].replace("\n","") in temp01.replace("\n",""): #筛选:替换掉原list中已有,但被包含之
call_relations[i] = temp01
may_replace = True
break
elif temp01.replace("\n","") in call_relations[i].replace("\n",""): #如果现在小于原已有之list,则忽略
may_replace = True
break
if not may_replace:
call_relations.append(temp01)
# logf = open(logfile, 'a')
# logf.write(temp03+"\n")
# logf.close()
# print(call_relations)
# ----------------------------------------------------------
# ----------------------------------------------------------
# logf = open(logfile, 'a')
# logf.write("%s.py.%s%s<--%s<--%s\n" % (func.__module__, func.__name__,infos, current_tname, current_pid))
# temp01 = "%s.%s" % (current_pid, current_tname)
# temp02 = "%s.py.%s%s\n" % (func.__module__, func.__name__,infos)
# temp03 = (temp01,temp02)
# call_relations.append(temp03)
# logf.close()
# print(infos)
#---------------------------------------------------------------
# frame=sys._getframe()
# code = frame.f_back.f_code
# filename=code.co_filename
# filebasename=os.path.basename(filename)
# caller_func=code.co_name
# lineno=frame.f_back.f_lineno
# current_pid = os.getpid()
# current_tname = threading.current_thread().name
# logf = open(logfile, 'a')
# logf.write("%s,%s,%s,%s,%s,%s,%s\n" % (filebasename, caller_func, lineno, current_pid, current_tname, func.__module__, func.__name__))
# logf.close()
#-------以下depth 0 会返回decorator信息----------------------------
# frame=sys._getframe(depth)
# code = frame.f_code
# filename=code.co_filename
# filebasename=os.path.basename(filename)
# caller_func=code.co_name
# lineno=code.co_firstlineno
#---------------------------------------------------------------
result = func(*args, **kwargs) # https://stackoverflow.com/questions/47249990/cant-get-the-parameters-of-a-func-using-threading-lib-in-a-python-decoration/47250184#47250184
# print("leave_func_name_-call_relations_len:%s" % len(call_relations))
return result
return wrapper
@atexit.register
def atexitFunc_3():
logf = open(logfile, 'a')
logf.writelines(call_relations)
logf.close()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化