本文整理汇总了Python中weakref.WeakKeyDictionary.itervalues方法的典型用法代码示例。如果您正苦于以下问题:Python WeakKeyDictionary.itervalues方法的具体用法?Python WeakKeyDictionary.itervalues怎么用?Python WeakKeyDictionary.itervalues使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weakref.WeakKeyDictionary
的用法示例。
在下文中一共展示了WeakKeyDictionary.itervalues方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: LazyInvalidation
# 需要导入模块: from weakref import WeakKeyDictionary [as 别名]
# 或者: from weakref.WeakKeyDictionary import itervalues [as 别名]
class LazyInvalidation(object):
def __enter__(self):
assert threading.current_thread() == MAIN_THREAD
assert not evaluation_stack
self._watchMap = WeakKeyDictionary()
self._watchable_objects = WeakSet()
global invalidation_strategy
invalidation_strategy._invalidate_all()
invalidation_strategy = self
def _watch_object(self, object):
if object.watcher is not None and object.watcher not in self._watchMap:
self._watchMap[object.watcher] = WeakWatchIntermediary(object, object.watcher)
def _add_dependency(self, object):
if evaluation_stack:
evaluation_stack[-1].deps.append(object)
def _unwatch_object(self, object):
object.invalidate()
self._watchable_objects.discard(object)
def __exit__(self, type, value, traceback):
global invalidation_strategy
invalidation_strategy = LazyConstants()
for intermediary in self._watchMap.itervalues():
intermediary.release()
self._watchMap.clear()
def _invalidate_all(self):
raise TypeError('Cannot nest lazy_invalidation contexts')
示例2: DebugMemoryPool
# 需要导入模块: from weakref import WeakKeyDictionary [as 别名]
# 或者: from weakref.WeakKeyDictionary import itervalues [as 别名]
class DebugMemoryPool(DeviceMemoryPool):
def __init__(self, interactive=True, logfile=None):
DeviceMemoryPool.__init__(self)
self.last_free, _ = cuda.mem_get_info()
self.interactive = interactive
if logfile is None:
import sys
logfile = sys.stdout
self.logfile = logfile
from weakref import WeakKeyDictionary
self.blocks = WeakKeyDictionary()
if interactive:
from pytools.diskdict import DiskDict
self.stacktrace_mnemonics = DiskDict("pycuda-stacktrace-mnemonics")
def allocate(self, size):
from traceback import extract_stack
stack = tuple(frm[2] for frm in extract_stack())
description = self.describe(stack, size)
histogram = {}
for bsize, descr in self.blocks.itervalues():
histogram[bsize, descr] = histogram.get((bsize, descr), 0) + 1
from pytools import common_prefix
cpfx = common_prefix(descr for bsize, descr in histogram)
print >> self.logfile, \
"\n Allocation of size %d occurring " \
"(mem: last_free:%d, free: %d, total:%d) (pool: held:%d, active:%d):" \
"\n at: %s" % (
(size, self.last_free)
+ cuda.mem_get_info()
+ (self.held_blocks, self.active_blocks,
description))
hist_items = sorted(list(histogram.iteritems()))
for (bsize, descr), count in hist_items:
print >> self.logfile, \
" %s (%d bytes): %dx" % (descr[len(cpfx):], bsize, count)
if self.interactive:
raw_input(" [Enter]")
result = DeviceMemoryPool.allocate(self, size)
self.blocks[result] = size, description
self.last_free, _ = cuda.mem_get_info()
return result
def describe(self, stack, size):
if not self.interactive:
return "|".join(stack)
else:
try:
return self.stacktrace_mnemonics[stack, size]
except KeyError:
print size, stack
while True:
mnemonic = raw_input("Enter mnemonic or [Enter] for more info:")
if mnemonic == '':
from traceback import print_stack
print_stack()
else:
break
self.stacktrace_mnemonics[stack, size] = mnemonic
return mnemonic