本文整理汇总了Python中weakref.WeakSet方法的典型用法代码示例。如果您正苦于以下问题:Python weakref.WeakSet方法的具体用法?Python weakref.WeakSet怎么用?Python weakref.WeakSet使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weakref
的用法示例。
在下文中一共展示了weakref.WeakSet方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: on
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakSet [as 别名]
def on(event, callback):
"""Call `callback` on `event`
Register `callback` to be run when `event` occurs.
Example:
>>> def on_init():
... print("Init happened")
...
>>> on("init", on_init)
>>> del on_init
Arguments:
event (str): Name of event
callback (callable): Any callable
"""
if event not in _registered_event_handlers:
_registered_event_handlers[event] = weakref.WeakSet()
events = _registered_event_handlers[event]
events.add(callback)
示例2: test_leaks
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakSet [as 别名]
def test_leaks(self):
def _get_items():
items, stops = [], []
for creator in _CREATORS:
item, stop = creator()
items.append(item)
stops.append(stop)
[stop() for stop in stops]
return weakref.WeakSet(items)
items = _get_items()
_pause()
# If this next line were uncommented, it would work without all the
# weakrefs in the code.
# gc.collect()
self.assertEqual(list(items), [])
示例3: test_multi_ordered_dict
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakSet [as 别名]
def test_multi_ordered_dict():
ref = _MultiOrderedDict_NSquared()
test = MultiOrderedDict()
ref_wset = weakref.WeakSet()
test_wset = weakref.WeakSet()
rng = np.random.RandomState()
tests = [
_test_a,
_test_b,
_test_c,
_test_d,
_test_e,
_test_f,
_test_g,
_test_h,
]
for _ in range(5000):
i = rng.randint(0, len(tests))
tests[i](ref, test, ref_wset, test_wset, rng, False)
示例4: __init__
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakSet [as 别名]
def __init__(
self, token: CancelToken = None, loop: asyncio.AbstractEventLoop = None
) -> None:
self.events = ServiceEvents()
self._run_lock = asyncio.Lock()
self._child_services = WeakSet()
self._tasks = WeakSet()
self._finished_callbacks = []
self._loop = loop
base_token = CancelToken(type(self).__name__, loop=loop)
if token is None:
self.cancel_token = base_token
else:
self.cancel_token = base_token.chain(token)
示例5: __init__
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakSet [as 别名]
def __init__(self,
url,
pooling=hgvs.global_config.uta.pooling,
application_name=None,
mode=None,
cache=None):
if url.schema is None:
raise Exception("No schema name provided in {url}".format(url=url))
self.application_name = application_name
self.pooling = pooling
self._conn = None
# If we're using connection pooling, track the set of DB
# connections we've seen; on first use we set the schema
# search path. Use weak references to avoid keeping connection
# objects alive unnecessarily.
self._conns_seen = weakref.WeakSet()
super(UTA_postgresql, self).__init__(url, mode, cache)
示例6: test_memory_usage
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakSet [as 别名]
def test_memory_usage(sentry_init, capture_events, args, expected_refcount):
sentry_init(**args)
references = weakref.WeakSet()
with start_transaction(name="hi"):
for i in range(100):
with start_span(op="helloworld", description="hi {}".format(i)) as span:
def foo():
pass
references.add(foo)
span.set_tag("foo", foo)
pass
del foo
del span
# required only for pypy (cpython frees immediately)
gc.collect()
assert len(references) == expected_refcount
示例7: test_union
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakSet [as 别名]
def test_union(self):
u = self.s.union(self.items2)
for c in self.letters:
self.assertEqual(c in u, c in self.d or c in self.items2)
self.assertEqual(self.s, WeakSet(self.items))
self.assertEqual(type(u), WeakSet)
self.assertRaises(TypeError, self.s.union, [[]])
for C in set, frozenset, dict.fromkeys, list, tuple:
x = WeakSet(self.items + self.items2)
c = C(self.items2)
self.assertEqual(self.s.union(c), x)
del c
self.assertEqual(len(u), len(self.items) + len(self.items2))
self.items2.pop()
gc.collect()
self.assertEqual(len(u), len(self.items) + len(self.items2))
示例8: test_len_cycles
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakSet [as 别名]
def test_len_cycles(self):
N = 20
items = [RefCycle() for i in range(N)]
s = WeakSet(items)
del items
it = iter(s)
try:
next(it)
except StopIteration:
pass
gc.collect()
n1 = len(s)
del it
gc.collect()
n2 = len(s)
# one item may be kept alive inside the iterator
self.assertIn(n1, (0, 1))
self.assertEqual(n2, 0)
示例9: test_len_race
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakSet [as 别名]
def test_len_race(self):
# Extended sanity checks for len() in the face of cyclic collection
self.addCleanup(gc.set_threshold, *gc.get_threshold())
for th in range(1, 100):
N = 20
gc.collect(0)
gc.set_threshold(th, th, th)
items = [RefCycle() for i in range(N)]
s = WeakSet(items)
del items
# All items will be collected at next garbage collection pass
it = iter(s)
try:
next(it)
except StopIteration:
pass
n1 = len(s)
del it
n2 = len(s)
self.assertGreaterEqual(n1, 0)
self.assertLessEqual(n1, N)
self.assertGreaterEqual(n2, 0)
self.assertLessEqual(n2, n1)
示例10: __init__
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakSet [as 别名]
def __init__(self, cls, has_weakref=True):
self._refs = weakref.WeakSet()
# Create a weak referenceable subclass if necessary
if not has_weakref:
class __weakref_cls__(cls):
__slots__ = ('__weakref__',)
else:
__weakref_cls__ = cls
def override_new(original_cls, *args, **kwargs):
"""Function replacing new allowing to track instances.
"""
new = __weakref_cls__.__old_new__(__weakref_cls__, *args, **kwargs)
self._refs.add(new)
return new
__weakref_cls__.__old_new__ = cls.__new__
cls.__new__ = override_new
__weakref_cls__.original_cls = cls
self.cls = __weakref_cls__
示例11: connect
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakSet [as 别名]
def connect(self, slot, sender=None):
if sender:
if inspect.ismethod(slot):
if sender not in self._methods_subs:
self._methods_subs[sender] = weakref.WeakKeyDictionary()
if slot.__self__ not in self._methods_subs[sender]:
self._methods_subs[sender][slot.__self__] = set()
self._methods_subs[sender][slot.__self__].add(slot.__func__)
else:
if sender not in self._functions_subs:
self._functions_subs[sender] = weakref.WeakSet()
self._functions_subs[sender].add(slot)
else:
if inspect.ismethod(slot):
if slot.__self__ not in self._methods:
self._methods[slot.__self__] = set()
self._methods[slot.__self__].add(slot.__func__)
else:
self._functions.add(slot)
示例12: __init__
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakSet [as 别名]
def __init__(self, nethost: str, netport: int, auto_shutdown: bool,
max_protocol: Tuple[int, int], **kwargs):
super().__init__(**kwargs)
self._nethost = nethost
self._netport = netport
self._edgecon_id = 0
self._num_connections = 0
self._servers = []
self._backends = weakref.WeakSet()
self._auto_shutdown = auto_shutdown
self._accepting = False
self._max_protocol = max_protocol
示例13: __init__
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakSet [as 别名]
def __init__(self, con, hdfs_client=None, **params):
import hdfs
self.con = con
if isinstance(hdfs_client, hdfs.Client):
hdfs_client = WebHDFS(hdfs_client)
elif hdfs_client is not None and not isinstance(hdfs_client, HDFS):
raise TypeError(hdfs_client)
self._hdfs = hdfs_client
self._kudu = None
self._temp_objects = weakref.WeakSet()
self._ensure_temp_db_exists()
示例14: save_weakset
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakSet [as 别名]
def save_weakset(self, obj):
self.save_reduce(weakref.WeakSet, (list(obj),))
示例15: stream_app_ctx
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakSet [as 别名]
def stream_app_ctx(app: web.Application) -> AsyncIterator[None]:
app['stream_pty_handlers'] = defaultdict(weakref.WeakSet)
app['stream_execute_handlers'] = defaultdict(weakref.WeakSet)
app['stream_proxy_handlers'] = defaultdict(weakref.WeakSet)
app['stream_stdin_socks'] = defaultdict(weakref.WeakSet)
app['zctx'] = zmq.asyncio.Context()
event_dispatcher = app['event_dispatcher']
event_dispatcher.subscribe('kernel_terminated', app, kernel_terminated)
yield
cancelled_tasks: List[asyncio.Task] = []
for per_kernel_handlers in app['stream_pty_handlers'].values():
for handler in list(per_kernel_handlers):
if not handler.done():
handler.cancel()
cancelled_tasks.append(handler)
for per_kernel_handlers in app['stream_execute_handlers'].values():
for handler in list(per_kernel_handlers):
if not handler.done():
handler.cancel()
cancelled_tasks.append(handler)
for per_kernel_handlers in app['stream_proxy_handlers'].values():
for handler in list(per_kernel_handlers):
if not handler.done():
handler.cancel()
cancelled_tasks.append(handler)
await asyncio.gather(*cancelled_tasks, return_exceptions=True)
app['zctx'].term()