本文整理匯總了Python中weakref.ref方法的典型用法代碼示例。如果您正苦於以下問題:Python weakref.ref方法的具體用法?Python weakref.ref怎麽用?Python weakref.ref使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類weakref
的用法示例。
在下文中一共展示了weakref.ref方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: import weakref [as 別名]
# 或者: from weakref import ref [as 別名]
def __init__(self, obj, func, *args, **kwargs):
if not self._registered_with_atexit:
# We may register the exit function more than once because
# of a thread race, but that is harmless
import atexit
atexit.register(self._exitfunc)
finalize._registered_with_atexit = True
info = self._Info()
info.weakref = ref(obj, self)
info.func = func
info.args = args
info.kwargs = kwargs or None
info.atexit = True
info.index = next(self._index_iter)
self._registry[self] = info
finalize._dirty = True
示例2: create_dict
# 需要導入模塊: import weakref [as 別名]
# 或者: from weakref import ref [as 別名]
def create_dict(self):
"""Create a new dict for the current thread, and return it."""
localdict = {}
key = self.key
thread = current_thread()
idt = id(thread)
def local_deleted(_, key=key):
# When the localimpl is deleted, remove the thread attribute.
thread = wrthread()
if thread is not None:
del thread.__dict__[key]
def thread_deleted(_, idt=idt):
# When the thread is deleted, remove the local dict.
# Note that this is suboptimal if the thread object gets
# caught in a reference loop. We would like to be called
# as soon as the OS-level thread ends instead.
local = wrlocal()
if local is not None:
dct = local.dicts.pop(idt)
wrlocal = ref(self, local_deleted)
wrthread = ref(thread, thread_deleted)
thread.__dict__[key] = wrlocal
self.dicts[idt] = wrthread, localdict
return localdict
示例3: subscribe
# 需要導入模塊: import weakref [as 別名]
# 或者: from weakref import ref [as 別名]
def subscribe(func):
'''
Add a subscriber function to option events
Parameters
----------
func : callable
A callable object that takes two parameters: key and value.
This function is called with the name and value of any option
that is set.
Returns
-------
None
'''
if isinstance(func, types.MethodType):
obj = six.get_method_self(func)
func = six.get_method_function(func)
_subscribers[func] = (weakref.ref(func), weakref.ref(obj))
else:
_subscribers[func] = (weakref.ref(func), None)
示例4: ensure_proc_terminate
# 需要導入模塊: import weakref [as 別名]
# 或者: from weakref import ref [as 別名]
def ensure_proc_terminate(proc):
"""
Make sure processes terminate when main process exit.
Args:
proc (multiprocessing.Process or list)
"""
if isinstance(proc, list):
for p in proc:
ensure_proc_terminate(p)
return
def stop_proc_by_weak_ref(ref):
proc = ref()
if proc is None:
return
if not proc.is_alive():
return
proc.terminate()
proc.join()
assert isinstance(proc, mp.Process)
atexit.register(stop_proc_by_weak_ref, weakref.ref(proc))
示例5: lex
# 需要導入模塊: import weakref [as 別名]
# 或者: from weakref import ref [as 別名]
def lex(self, source, name=None, filename=None):
"""Lex the given sourcecode and return a generator that yields
tokens as tuples in the form ``(lineno, token_type, value)``.
This can be useful for :ref:`extension development <writing-extensions>`
and debugging templates.
This does not perform preprocessing. If you want the preprocessing
of the extensions to be applied you have to filter source through
the :meth:`preprocess` method.
"""
source = text_type(source)
try:
return self.lexer.tokeniter(source, name, filename)
except TemplateSyntaxError:
exc_info = sys.exc_info()
self.handle_exception(exc_info, source_hint=source)
示例6: _adjust_thread_count
# 需要導入模塊: import weakref [as 別名]
# 或者: from weakref import ref [as 別名]
def _adjust_thread_count(self):
# if idle threads are available, don't spin new threads
if self._idle_semaphore.acquire(False):
return
# When the executor gets lost, the weakref callback will wake up
# the worker threads.
def weakref_cb(_, q=self._work_queue):
q.put(None)
num_threads = len(self._threads)
if num_threads < self._max_workers:
thread_name = '%s_%d' % (self._thread_name_prefix or self,
num_threads)
t = threading.Thread(name=thread_name, target=_worker,
args=(weakref.ref(self, weakref_cb),
self._work_queue))
t.daemon = True
t.start()
self._threads.add(t)
_threads_queues[t] = self._work_queue
示例7: _start_queue_management_thread
# 需要導入模塊: import weakref [as 別名]
# 或者: from weakref import ref [as 別名]
def _start_queue_management_thread(self):
# When the executor gets lost, the weakref callback will wake up
# the queue management thread.
def weakref_cb(_, q=self._result_queue):
q.put(None)
if self._queue_management_thread is None:
self._queue_management_thread = threading.Thread(
target=_queue_management_worker,
args=(weakref.ref(self, weakref_cb),
self._processes,
self._pending_work_items,
self._work_ids,
self._call_queue,
self._result_queue))
self._queue_management_thread.daemon = True
self._queue_management_thread.start()
_threads_queues[self._queue_management_thread] = self._result_queue
示例8: test_task_refcounting
# 需要導入模塊: import weakref [as 別名]
# 或者: from weakref import ref [as 別名]
def test_task_refcounting(self):
# On CPython, tasks and their arguments should be released immediately
# without waiting for garbage collection.
@gen.engine
def f():
class Foo(object):
pass
arg = Foo()
self.arg_ref = weakref.ref(arg)
task = gen.Task(self.io_loop.add_callback, arg=arg)
self.task_ref = weakref.ref(task)
yield task
self.stop()
self.run_gen(f)
self.assertIs(self.arg_ref(), None)
self.assertIs(self.task_ref(), None)
示例9: _adjust_thread_count
# 需要導入模塊: import weakref [as 別名]
# 或者: from weakref import ref [as 別名]
def _adjust_thread_count(self):
# When the executor gets lost, the weakref callback will wake up
# the worker threads.
def weakref_cb(_, q=self._work_queue):
q.put(None)
# TODO(bquinlan): Should avoid creating new threads if there are more
# idle threads than items in the work queue.
num_threads = len(self._threads)
if num_threads < self._max_workers:
thread_name = '%s_%d' % (self._thread_name_prefix or self,
num_threads)
t = threading.Thread(name=thread_name, target=_worker,
args=(weakref.ref(self, weakref_cb),
self._work_queue))
t.daemon = True
t.start()
self._threads.add(t)
_threads_queues[t] = self._work_queue
示例10: attach
# 需要導入模塊: import weakref [as 別名]
# 或者: from weakref import ref [as 別名]
def attach(self, hooks_dict, hook):
assert not self._hooks_dict_ref, 'The same handle cannot be attached twice.'
self._id = id(hook)
hooks_dict[self._id] = hook
self._hooks_dict_ref = weakref.ref(hooks_dict)
示例11: __setstate__
# 需要導入模塊: import weakref [as 別名]
# 或者: from weakref import ref [as 別名]
def __setstate__(self, state):
if state[0] is None:
self._hooks_dict_ref = weakref.ref(collections.OrderedDict())
else:
self._hooks_dict_ref = weakref.ref(state[0])
self._id = state[1]
示例12: _create_accelerator
# 需要導入模塊: import weakref [as 別名]
# 或者: from weakref import ref [as 別名]
def _create_accelerator(self, builder, traverser):
obj = self._native.create_accelerator(builder, traverser)
self._destroyables.append(weakref.ref(obj))
return obj
示例13: _create_geometry
# 需要導入模塊: import weakref [as 別名]
# 或者: from weakref import ref [as 別名]
def _create_geometry(self):
obj = self._native.create_geometry()
self._destroyables.append(weakref.ref(obj))
return obj
示例14: _create_buffer
# 需要導入模塊: import weakref [as 別名]
# 或者: from weakref import ref [as 別名]
def _create_buffer(self, buffer_type):
obj = self._native.create_buffer(buffer_type)
self._destroyables.append(weakref.ref(obj))
return obj
示例15: _create_geometry_group
# 需要導入模塊: import weakref [as 別名]
# 或者: from weakref import ref [as 別名]
def _create_geometry_group(self):
obj = self._native.create_geometry_group()
self._destroyables.append(weakref.ref(obj))
return obj