def g():
marker = object()
yield marker
# now the marker is in the tuple being constructed
[tup] = [x for x in gc.get_referrers(marker) if type(x) is tuple]
print tup
print tup[1]
python类get_referrers()的实例源码
def file_module_function_of(self, frame):
code = frame.f_code
filename = code.co_filename
if filename:
modulename = modname(filename)
else:
modulename = None
funcname = code.co_name
clsname = None
if code in self._caller_cache:
if self._caller_cache[code] is not None:
clsname = self._caller_cache[code]
else:
self._caller_cache[code] = None
## use of gc.get_referrers() was suggested by Michael Hudson
# all functions which refer to this code object
funcs = [f for f in gc.get_referrers(code)
if inspect.isfunction(f)]
# require len(func) == 1 to avoid ambiguity caused by calls to
# new.function(): "In the face of ambiguity, refuse the
# temptation to guess."
if len(funcs) == 1:
dicts = [d for d in gc.get_referrers(funcs[0])
if isinstance(d, dict)]
if len(dicts) == 1:
classes = [c for c in gc.get_referrers(dicts[0])
if hasattr(c, "__bases__")]
if len(classes) == 1:
# ditto for new.classobj()
clsname = classes[0].__name__
# cache the result - assumption is that new.* is
# not called later to disturb this relationship
# _caller_cache could be flushed if functions in
# the new module get called.
self._caller_cache[code] = clsname
if clsname is not None:
funcname = "%s.%s" % (clsname, funcname)
return filename, modulename, funcname
def file_module_function_of(self, frame):
code = frame.f_code
filename = code.co_filename
if filename:
modulename = modname(filename)
else:
modulename = None
funcname = code.co_name
clsname = None
if code in self._caller_cache:
if self._caller_cache[code] is not None:
clsname = self._caller_cache[code]
else:
self._caller_cache[code] = None
## use of gc.get_referrers() was suggested by Michael Hudson
# all functions which refer to this code object
funcs = [f for f in gc.get_referrers(code)
if inspect.isfunction(f)]
# require len(func) == 1 to avoid ambiguity caused by calls to
# new.function(): "In the face of ambiguity, refuse the
# temptation to guess."
if len(funcs) == 1:
dicts = [d for d in gc.get_referrers(funcs[0])
if isinstance(d, dict)]
if len(dicts) == 1:
classes = [c for c in gc.get_referrers(dicts[0])
if hasattr(c, "__bases__")]
if len(classes) == 1:
# ditto for new.classobj()
clsname = classes[0].__name__
# cache the result - assumption is that new.* is
# not called later to disturb this relationship
# _caller_cache could be flushed if functions in
# the new module get called.
self._caller_cache[code] = clsname
if clsname is not None:
funcname = "%s.%s" % (clsname, funcname)
return filename, modulename, funcname
def run_periodic_checks(self):
ts = arrow.utcnow().timestamp
def deferreds():
return len(gc.get_referrers(Deferred))
def rss_mb():
rss = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1024
if sys.platform.startswith('darwin'):
rss /= 1024
return rss
kpi_event = KpiEvent(
type=KpiEventType.slice,
ts=ts,
prefixes={
'voltha.internal.{}'.format(self.instance_id):
MetricValuePairs(metrics={
'deferreds': deferreds(),
'rss-mb': rss_mb(),
})
}
)
self.event_bus.publish('kpis', kpi_event)
log.debug('periodic-check', ts=ts)
def __getstate__(self):
log.msg( "WARNING: serializing ephemeral %s" % self )
if not _PYPY:
import gc
if getattr(gc, 'get_referrers', None):
for r in gc.get_referrers(self):
log.msg( " referred to by %s" % (r,))
return None
def g():
marker = object()
yield marker
# now the marker is in the tuple being constructed
[tup] = [x for x in gc.get_referrers(marker) if type(x) is tuple]
print(tup)
print(tup[1])
def file_module_function_of(self, frame):
code = frame.f_code
filename = code.co_filename
if filename:
modulename = _modname(filename)
else:
modulename = None
funcname = code.co_name
clsname = None
if code in self._caller_cache:
if self._caller_cache[code] is not None:
clsname = self._caller_cache[code]
else:
self._caller_cache[code] = None
## use of gc.get_referrers() was suggested by Michael Hudson
# all functions which refer to this code object
funcs = [f for f in gc.get_referrers(code)
if inspect.isfunction(f)]
# require len(func) == 1 to avoid ambiguity caused by calls to
# new.function(): "In the face of ambiguity, refuse the
# temptation to guess."
if len(funcs) == 1:
dicts = [d for d in gc.get_referrers(funcs[0])
if isinstance(d, dict)]
if len(dicts) == 1:
classes = [c for c in gc.get_referrers(dicts[0])
if hasattr(c, "__bases__")]
if len(classes) == 1:
# ditto for new.classobj()
clsname = classes[0].__name__
# cache the result - assumption is that new.* is
# not called later to disturb this relationship
# _caller_cache could be flushed if functions in
# the new module get called.
self._caller_cache[code] = clsname
if clsname is not None:
funcname = "%s.%s" % (clsname, funcname)
return filename, modulename, funcname
def run_and_check(run_client):
w = run_interaction(run_client=run_client)
# clear_sys_exc_info()
gc.collect()
fd = w()
print('run_and_check: weakref fd:', fd)
if fd:
print(pprint.pformat(gc.get_referrers(fd)))
for x in gc.get_referrers(fd):
print(pprint.pformat(x))
for y in gc.get_referrers(x):
print('- {0}'.format(pprint.pformat(y)))
raise AssertionError('server should be dead by now')
def test_old_callback_forgotten(self):
"""
If :py:obj:`Context.set_tlsext_servername_callback` is used to specify a new
callback, the one it replaces is dereferenced.
"""
def callback(connection):
pass
def replacement(connection):
pass
context = Context(TLSv1_METHOD)
context.set_tlsext_servername_callback(callback)
tracker = ref(callback)
del callback
context.set_tlsext_servername_callback(replacement)
# One run of the garbage collector happens to work on CPython. PyPy
# doesn't collect the underlying object until a second run for whatever
# reason. That's fine, it still demonstrates our code has properly
# dropped the reference.
collect()
collect()
callback = tracker()
if callback is not None:
referrers = get_referrers(callback)
if len(referrers) > 1:
self.fail("Some references remain: %r" % (referrers,))
def test_save_load_proj(self):
def namestr(obj, namespace): # return string of gc.referrers
return [name for name in namespace if namespace[name] is obj]
# check that nothing else has created a ref to changes (no circular)
changes = self.proj.get_changes()
assert len(gc.get_referrers(changes)) == 1 # just one ref (ours!)
def ascend(self, obj, depth=1):
"""Return a nested list containing referrers of the given object."""
depth += 1
parents = []
# Gather all referrers in one step to minimize
# cascading references due to repr() logic.
refs = gc.get_referrers(obj)
self.ignore.append(refs)
if len(refs) > self.maxparents:
return [("[%s referrers]" % len(refs), [])]
try:
ascendcode = self.ascend.__code__
except AttributeError:
ascendcode = self.ascend.im_func.func_code
for parent in refs:
if inspect.isframe(parent) and parent.f_code is ascendcode:
continue
if parent in self.ignore:
continue
if depth <= self.maxdepth:
parents.append((parent, self.ascend(parent, depth)))
else:
parents.append((parent, []))
return parents
def invalidate_module(name):
global modules
global __debug
for item in modules.keys():
if item == name or item.startswith(name+'.'):
dprint('Remove {} from pupyimporter.modules'.format(item))
del modules[item]
for item in sys.modules.keys():
if not (item == name or item.startswith(name+'.')):
continue
mid = id(sys.modules[item])
dprint('Remove {} from sys.modules'.format(item))
del sys.modules[item]
if hasattr(pupy, 'namespace'):
dprint('Remove {} from rpyc namespace'.format(item))
pupy.namespace.__invalidate__(item)
if __debug:
for obj in gc.get_objects():
if id(obj) == mid:
dprint('Module {} still referenced by {}'.format(
item, [ id(x) for x in gc.get_referrers(obj) ]
))
gc.collect()
def searchRefs(obj, *args):
"""Pseudo-interactive function for tracing references backward.
**Arguments:**
obj: The initial object from which to start searching
args: A set of string or int arguments.
each integer selects one of obj's referrers to be the new 'obj'
each string indicates an action to take on the current 'obj':
t: print the types of obj's referrers
l: print the lengths of obj's referrers (if they have __len__)
i: print the IDs of obj's referrers
o: print obj
ro: return obj
rr: return list of obj's referrers
Examples::
searchRefs(obj, 't') ## Print types of all objects referring to obj
searchRefs(obj, 't', 0, 't') ## ..then select the first referrer and print the types of its referrers
searchRefs(obj, 't', 0, 't', 'l') ## ..also print lengths of the last set of referrers
searchRefs(obj, 0, 1, 'ro') ## Select index 0 from obj's referrer, then select index 1 from the next set of referrers, then return that object
"""
ignore = {id(sys._getframe()): None}
gc.collect()
refs = gc.get_referrers(obj)
ignore[id(refs)] = None
refs = [r for r in refs if id(r) not in ignore]
for a in args:
#fo = allFrameObjs()
#refs = [r for r in refs if r not in fo]
if type(a) is int:
obj = refs[a]
gc.collect()
refs = gc.get_referrers(obj)
ignore[id(refs)] = None
refs = [r for r in refs if id(r) not in ignore]
elif a == 't':
print(list(map(typeStr, refs)))
elif a == 'i':
print(list(map(id, refs)))
elif a == 'l':
def slen(o):
if hasattr(o, '__len__'):
return len(o)
else:
return None
print(list(map(slen, refs)))
elif a == 'o':
print(obj)
elif a == 'ro':
return obj
elif a == 'rr':
return refs
def searchRefs(obj, *args):
"""Pseudo-interactive function for tracing references backward.
**Arguments:**
obj: The initial object from which to start searching
args: A set of string or int arguments.
each integer selects one of obj's referrers to be the new 'obj'
each string indicates an action to take on the current 'obj':
t: print the types of obj's referrers
l: print the lengths of obj's referrers (if they have __len__)
i: print the IDs of obj's referrers
o: print obj
ro: return obj
rr: return list of obj's referrers
Examples::
searchRefs(obj, 't') ## Print types of all objects referring to obj
searchRefs(obj, 't', 0, 't') ## ..then select the first referrer and print the types of its referrers
searchRefs(obj, 't', 0, 't', 'l') ## ..also print lengths of the last set of referrers
searchRefs(obj, 0, 1, 'ro') ## Select index 0 from obj's referrer, then select index 1 from the next set of referrers, then return that object
"""
ignore = {id(sys._getframe()): None}
gc.collect()
refs = gc.get_referrers(obj)
ignore[id(refs)] = None
refs = [r for r in refs if id(r) not in ignore]
for a in args:
#fo = allFrameObjs()
#refs = [r for r in refs if r not in fo]
if type(a) is int:
obj = refs[a]
gc.collect()
refs = gc.get_referrers(obj)
ignore[id(refs)] = None
refs = [r for r in refs if id(r) not in ignore]
elif a == 't':
print(list(map(typeStr, refs)))
elif a == 'i':
print(list(map(id, refs)))
elif a == 'l':
def slen(o):
if hasattr(o, '__len__'):
return len(o)
else:
return None
print(list(map(slen, refs)))
elif a == 'o':
print(obj)
elif a == 'ro':
return obj
elif a == 'rr':
return refs
def _memory_dump(opts):
for typ, n in objgraph.most_common_types():
logging.info('{typ:30} {n:>10}'.format(typ=typ, n=n))
objects = []
rng = opts['size_range']
summ = {
'max_refsize': {
'size': 0,
},
}
for obj in gc.get_objects():
if not hasattr(obj, '__class__'):
continue
size = sys.getsizeof(obj, 0)
if rng is not None:
if not (rng[0] <= size < rng[1]):
continue
i = id(obj)
# referrers = [id(o)
# for o in gc.get_referrers(obj)
# if hasattr(o, '__class__')]
referents = [(id(o), _get_class(o), sys.getsizeof(o, 0))
for o in gc.get_referents(obj)
# if hasattr(o, '__class__')
]
refsize = sum([x[2] for x in referents])
cls = _get_class(obj)
data = [
i,
cls,
size, # object size
refsize, # size of all direct referents
referents, # referents
]
objects.append(data)
if summ['max_refsize']['size'] < refsize:
summ['max_refsize'] = {
'size': refsize,
'object': data,
}
for o in objects:
logging.info('memory-dump: ' + json.dumps(o))
logging.info('memory-dump summary: ' + json.dumps(summ))
def file_module_function_of(self, frame):
code = frame.f_code
filename = code.co_filename
if filename:
modulename = _modname(filename)
else:
modulename = None
funcname = code.co_name
clsname = None
if code in self._caller_cache:
if self._caller_cache[code] is not None:
clsname = self._caller_cache[code]
else:
self._caller_cache[code] = None
## use of gc.get_referrers() was suggested by Michael Hudson
# all functions which refer to this code object
funcs = [f for f in gc.get_referrers(code)
if inspect.isfunction(f)]
# require len(func) == 1 to avoid ambiguity caused by calls to
# new.function(): "In the face of ambiguity, refuse the
# temptation to guess."
if len(funcs) == 1:
dicts = [d for d in gc.get_referrers(funcs[0])
if isinstance(d, dict)]
if len(dicts) == 0:
# PyPy may store functions directly on the class
# (more exactly: the container is not a Python object)
dicts = funcs
if len(dicts) == 1:
classes = [c for c in gc.get_referrers(dicts[0])
if hasattr(c, "__bases__")]
if len(classes) == 1:
# ditto for new.classobj()
clsname = classes[0].__name__
# cache the result - assumption is that new.* is
# not called later to disturb this relationship
# _caller_cache could be flushed if functions in
# the new module get called.
self._caller_cache[code] = clsname
if clsname is not None:
funcname = "%s.%s" % (clsname, funcname)
return filename, modulename, funcname
def file_module_function_of(self, frame):
code = frame.f_code
filename = code.co_filename
if filename:
modulename = modname(filename)
else:
modulename = None
funcname = code.co_name
clsname = None
if code in self._caller_cache:
if self._caller_cache[code] is not None:
clsname = self._caller_cache[code]
else:
self._caller_cache[code] = None
## use of gc.get_referrers() was suggested by Michael Hudson
# all functions which refer to this code object
funcs = [f for f in gc.get_referrers(code)
if inspect.isfunction(f)]
# require len(func) == 1 to avoid ambiguity caused by calls to
# new.function(): "In the face of ambiguity, refuse the
# temptation to guess."
if len(funcs) == 1:
dicts = [d for d in gc.get_referrers(funcs[0])
if isinstance(d, dict)]
if len(dicts) == 0:
# PyPy may store functions directly on the class
# (more exactly: the container is not a Python object)
dicts = funcs
if len(dicts) == 1:
classes = [c for c in gc.get_referrers(dicts[0])
if hasattr(c, "__bases__")]
if len(classes) == 1:
# ditto for new.classobj()
clsname = classes[0].__name__
# cache the result - assumption is that new.* is
# not called later to disturb this relationship
# _caller_cache could be flushed if functions in
# the new module get called.
self._caller_cache[code] = clsname
if clsname is not None:
funcname = "%s.%s" % (clsname, funcname)
return filename, modulename, funcname
def finalize(data, model=None, instance=None, results=None):
"""
Perform final actions to finish the execution of the pyomo script.
This function prints statistics related to the execution of the pyomo script.
Additionally, this function will drop into the python interpreter if the `interactive`
option is `True`.
Required:
model: A pyomo model object.
Optional:
instance: A problem instance derived from the model object.
results: Optimization results object.
"""
#
# Deactivate and delete plugins
#
##import gc
##print "HERE - usermodel_plugins"
##_tmp = data.options._usermodel_plugins[0]
cleanup()
# NOTE: This function gets called for cleanup during exceptions
# to prevent memory leaks. Don't reconfigure the loggers
# here or we will lose the exception information.
#configure_loggers(reset=True)
data.local._usermodel_plugins = []
##gc.collect()
##print gc.get_referrers(_tmp)
##import pyomo.core.base.plugin
##print pyomo.util.plugin.interface_services[pyomo.core.base.plugin.IPyomoScriptSaveResults]
##print "HERE - usermodel_plugins"
##
if not data.options.runtime.logging == 'quiet':
sys.stdout.write('[%8.2f] Pyomo Finished\n' % (time.time()-start_time))
if (pympler_available is True) and (data.options.runtime.profile_memory >= 1):
sys.stdout.write('Maximum memory used = %d bytes\n' % data.local.max_memory)
sys.stdout.flush()
#
model=model
instance=instance
results=results
#
if data.options.runtime.interactive:
if IPython_available:
ipshell()
else:
import code
shell = code.InteractiveConsole(locals())
print('\n# Dropping into Python interpreter')
shell.interact()
print('\n# Leaving Interpreter, back to Pyomo\n')