活动介绍
file-type

Python3标准库:深入理解weakref弱引用机制

版权申诉

DOCX文件

142KB | 更新于2024-08-08 | 52 浏览量 | 0 下载量 举报 收藏
download 限时特惠:#14.90
"Python3标准库的weakref模块提供了弱引用功能,用于处理对象的非永久性引用,避免循环引用和内存泄漏问题。弱引用不增加对象的引用计数,允许对象被垃圾回收机制自动清理。本文档详细介绍了弱引用的创建、引用回调以及对象的最终化处理。" 在Python中,`weakref`模块提供了处理弱引用的能力,这在特定情况下非常有用,特别是当需要防止对象因为循环引用而无法被垃圾回收时。弱引用不会阻止对象被删除,即使有弱引用指向该对象,只要没有强引用存在,对象仍然会被回收。 1. **弱引用对象的创建** 弱引用通过`weakref.ref`类创建。例如: ```python import weakref class ExpensiveObject: def __del__(self): print('(Deleting {})'.format(self)) obj = ExpensiveObject() r = weakref.ref(obj) ``` 在这个例子中,`r`是一个弱引用,可以通过`r()`获取原始对象。当原始对象`obj`被删除后,`r()`将返回`None`。 1. **引用回调** `weakref.ref`构造函数可以接受一个回调函数作为参数,当弱引用的对象被删除时,该回调函数会被调用。这对于跟踪对象的生命周期很有帮助: ```python def callback(reference): """Invoked when referenced object is deleted""" print('callback({!r})'.format(reference)) obj = ExpensiveObject() r = weakref.ref(obj, callback) ``` 当`obj`被删除时,`callback`函数将接收到`r`作为参数,表示弱引用对象已经不存在。 1. **对象的最终化处理** 对于更复杂的资源清理,`weakref.finalize`函数可以用来注册一个在对象被垃圾回收前执行的回调。这允许在对象即将消失时进行更精确的控制: ```python from weakref import finalize def cleanup(obj): print('Cleaning up {}'.format(obj)) obj = ExpensiveObject() finalize(obj, cleanup, obj) ``` `finalize`函数创建了一个`finalize`实例,它会在对象被垃圾回收时调用`cleanup`函数,并传入对象`obj`。 Python的`weakref`模块提供了优雅处理对象引用的方法,可以有效地解决循环引用问题,提高程序内存管理的效率,同时允许开发者在对象生命周期的关键时刻执行特定操作。在编写需要高效内存管理的代码时,了解并合理使用弱引用是十分重要的。

相关推荐

filetype

Traceback (most recent call last): File "E:\guowu0328\0329.py", line 107, in <module> viewer.update_plot() # 初始绘制 File "E:\guowu0328\0329.py", line 66, in update_plot contour = self.scene.mlab.contour3d( File "E:\guowu0328\.venv\lib\site-packages\mayavi\tools\helper_functions.py", line 38, in the_function return pipeline(*args, **kwargs) File "E:\guowu0328\.venv\lib\site-packages\mayavi\tools\helper_functions.py", line 83, in __call__ output = self.__call_internal__(*args, **kwargs) File "E:\guowu0328\.venv\lib\site-packages\mayavi\tools\helper_functions.py", line 93, in __call_internal__ self.source = self._source_function(*args, **kwargs) File "E:\guowu0328\.venv\lib\site-packages\mayavi\tools\sources.py", line 1168, in scalar_field data_source.reset(x=x, y=y, z=z, scalars=s) File "E:\guowu0328\.venv\lib\site-packages\mayavi\tools\sources.py", line 391, in reset ds = ArraySource(transpose_input_array=True) File "E:\guowu0328\.venv\lib\site-packages\mayavi\sources\array_source.py", line 153, in __init__ self.image_data) File "E:\guowu0328\.venv\lib\site-packages\traits\trait_types.py", line 3612, in create_default_value return klass(*args[1:], **kw) File "tvtk_classes\image_data.py", line 53, in __init__ File "E:\guowu0328\.venv\lib\site-packages\tvtk\tvtk_base.py", line 435, in __init__ self.setup_observers() File "E:\guowu0328\.venv\lib\site-packages\tvtk\tvtk_base.py", line 536, in setup_observers _object_cache.setup_observers(self._vtk_obj, File "E:\guowu0328\.venv\lib\site-packages\tvtk\tvtk_base.py", line 93, in setup_observers messenger.connect(vtk_obj, event, method) File "E:\guowu0328\.venv\lib\site-packages\tvtk\messenger.py", line 302, in connect _messenger.connect(obj, event, callback) File "E:\guowu0328\.venv\lib\site-packages\tvtk\messenger.py", line 148, in connect key = hash(obj) TypeError: unhashable type: 'ImageData'

filetype

INFO | open_webui.retrieval.models.external:predict:37 - ExternalReranker:predict:query 深圳河水质监测技术改进 - {} 2025-07-24 16:45:44 2025-07-24 16:45:44.142 | ERROR | open_webui.retrieval.models.external:predict:59 - Error in external reranking: 405 Client Error: Method Not Allowed for url: http://host.docker.internal:11434/ - {} 2025-07-24 16:45:44 Traceback (most recent call last): 2025-07-24 16:45:44 2025-07-24 16:45:44 File "/usr/local/lib/python3.11/threading.py", line 1002, in _bootstrap 2025-07-24 16:45:44 self._bootstrap_inner() 2025-07-24 16:45:44 │ └ <function Thread._bootstrap_inner at 0x786151af09a0> 2025-07-24 16:45:44 └ <Thread(ThreadPoolExecutor-10_1, started 132355104691904)> 2025-07-24 16:45:44 File "/usr/local/lib/python3.11/threading.py", line 1045, in _bootstrap_inner 2025-07-24 16:45:44 self.run() 2025-07-24 16:45:44 │ └ <function Thread.run at 0x786151af0680> 2025-07-24 16:45:44 └ <Thread(ThreadPoolExecutor-10_1, started 132355104691904)> 2025-07-24 16:45:44 File "/usr/local/lib/python3.11/threading.py", line 982, in run 2025-07-24 16:45:44 self._target(*self._args, **self._kwargs) 2025-07-24 16:45:44 │ │ │ │ │ └ {} 2025-07-24 16:45:44 │ │ │ │ └ <Thread(ThreadPoolExecutor-10_1, started 132355104691904)> 2025-07-24 16:45:44 │ │ │ └ (<weakref at 0x786082c8acf0; to 'ThreadPoolExecutor' at 0x786083728310>, <_queue.SimpleQueue object at 0x786082e0bf10>, None,... 2025-07-24 16:45:44 │ │ └ <Thread(ThreadPoolExecutor-10_1, started 132355104691904)> 2025-07-24 16:45:44 │ └ <function _worker at 0x786150bb2b60> 2025-07-24 16:45:44 └ <Thread(ThreadPoolExecutor-10_1, started 132355104691904)> 2025-07-24 16:45:44 File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 83, in _worker 2025-07-24 16:45:44 work_item.run() 2025-07-24 16:45:44 │ └ <function _WorkItem.run at 0x786150bb2ca0> 2025-07-24 16:45:44 └ <concurrent.futures.thread._WorkItem object at 0x786087361990> 2025-07-24 16:45:44 File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run 2025-07-24 16:45:44 result = self.fn(*self.args, **self.kwargs) 2025-07-24 16:45:44 │ │ │ │ │ └ {} 2025-07-24 16:45:44 │ │ │ │ └ <concurrent.futures.thread._WorkItem object at 0x786087361990> 2025-07-24 16:45:44 │ │ │ └ ('b5dcbf6d-5774-402d-9fff-5ec4906807ea', '深圳河水质监测技术改进') 2025-07-24 16:45:44 │ │ └ <concurrent.futures.thread._WorkItem object at 0x786087361990> 2025-07-24 16:45:44 │ └ <function query_collection_with_hybrid_search.<locals>.process_query at 0x78608378b100> 2025-07-24 16:45:44 └ <concurrent.futures.thread._WorkItem object at 0x786087361990> 2025-07-24 16:45:44 2025-07-24 16:45:44 File "/app/backend/open_webui/retrieval/utils.py", line 354, in process_query 2025-07-24 16:45:44 result = query_doc_with_hybrid_search( 2025-07-24 16:45:44 └ <function query_doc_with_hybrid_search at 0x78608d3b8220> 2025-07-24 16:45:44 2025-07-24 16:45:44 File "/app/backend/open_webui/retrieval/utils.py", line 161, in query_doc_with_hybrid_search 2025-07-24 16:45:44 result = compression_retriever.invoke(query) 2025-07-24 16:45:44 │ │ └ '深圳河水质监测技术改进' 2025-07-24 16:45:44 │ └ <function BaseRetriever.invoke at 0x78608d4d8a40> 2025-07-24 16:45:44 └ ContextualCompressionRetriever(base_compressor=RerankCompressor(embedding_function=<function chat_completion_files_handler.<l... 2025-07-24 16:45:44 2025-07-24 16:45:44 File "/usr/local/lib/python3.11/site-packages/langchain_core/retrievers.py", line 259, in invoke 2025-07-24 16:45:44 result = self._get_relevant_documents( 2025-07-24 16:45:44 │ └ <function ContextualCompressionRetriever._get_relevant_documents at 0x78608d4d87c0> 2025-07-24 16:45:44 └ ContextualCompressionRetriever(base_compressor=RerankCompressor(embedding_function=<function chat_completion_files_handler.<l... 2025-07-24 16:45:44 File "/usr/local/lib/python3.11/site-packages/langchain/retrievers/contextual_compression.py", line 48, in _get_relevant_documents 2025-07-24 16:45:44 compressed_docs = self.base_compressor.compress_documents( 2025-07-24 16:45:44 │ │ └ <function RerankCompressor.compress_documents at 0x78608d3ba520> 2025-07-24 16:45:44 │ └ RerankCompressor(embedding_function=<function chat_completion_files_handler.<locals>.<lambda>.<locals>.<lambda> at 0x78608378... 2025-07-24 16:45:44 └ ContextualCompressionRetriever(base_compressor=RerankCompressor(embedding_function=<function chat_completion_files_handler.<l... 2025-07-24 16:45:44 2025-07-24 16:45:44 File "/app/backend/open_webui/retrieval/utils.py", line 877, in compress_documents 2025-07-24 16:45:44 scores = self.reranking_function.predict( 2025-07-24 16:45:44 │ │ └ <function ExternalReranker.predict at 0x7860893142c0> 2025-07-24 16:45:44 │ └ <open_webui.retrieval.models.external.ExternalReranker object at 0x786082e78fd0> 2025-07-24 16:45:44 └ RerankCompressor(embedding_function=<function chat_completion_files_handler.<locals>.<lambda>.<locals>.<lambda> at 0x78608378... 2025-07-24 16:45:44 2025-07-24 16:45:44 > File "/app/backend/open_webui/retrieval/models/external.py", line 48, in predict 2025-07-24 16:45:44 r.raise_for_status() 2025-07-24 16:45:44 │ └ <function Response.raise_for_status at 0x78614e2a6ca0> 2025-07-24 16:45:44 └ <Response [405]> 2025-07-24 16:45:44 2025-07-24 16:45:44 File "/usr/local/lib/python3.11/site-packages/requests/models.py", line 1026, in raise_for_status 2025-07-24 16:45:44 raise HTTPError(http_error_msg, response=self) 2025-07-24 16:45:44 │ │ └ <Response [405]> 2025-07-24 16:45:44 │ └ '405 Client Error: Method Not Allowed for url: http://host.docker.internal:11434/' 2025-07-24 16:45:44 └ <class 'requests.exceptions.HTTPError'> 2025-07-24 16:45:44 2025-07-24 16:45:44 requests.exceptions.HTTPError: 405 Client Error: Method Not Allowed for url: http://host.docker.internal:11434/ 2025-07-24 16:45:44 2025-07-24 16:45:44.145 | ERROR | open_webui.retrieval.utils:query_doc_with_hybrid_search:187 - Error querying doc b5dcbf6d-5774-402d-9fff-5ec4906807ea with hybrid search: 'NoneType' object has no attribute 'tolist' - {}