� I�gO8��P�dZddlZddlZddlZddlmZ en#e$red��ZYnwxYwddl m Z ddl m Z ddl m Z ddlmZdd lmZdd lmZmZdd lmZ ddlZn #e$rdZYnwxYwGd �d e ��ZGd�de��ZGd�de��ZGd�de��ZdS)a�Custom implementation of multiprocessing.Pool with custom pickler. This module provides efficient ways of working with data stored in shared memory with numpy.memmap arrays without inducing any memory copy between the parent and child processes. This module should not be imported if multiprocessing is not available as it implements subclasses of multiprocessing Pool that uses a custom alternative to SimpleQueue. �N)�sleep)�Pickler)�HIGHEST_PROTOCOL)�BytesIO�)�get_memmapping_reducers)�TemporaryResourcesManager)�mp�assert_spawning)�Poolc�$�eZdZdZdefd�Zd�ZdS)�CustomizablePicklera�Pickler that accepts custom reducers. TODO python2_drop : can this be simplified ? HIGHEST_PROTOCOL is selected by default as this pickler is used to pickle ephemeral datastructures for interprocess communication hence no backward compatibility is required. `reducers` is expected to be a dictionary with key/values being `(type, callable)` pairs where `callable` is a function that give an instance of `type` will return a tuple `(constructor, tuple_of_objects)` to rebuild an instance out of the pickled `tuple_of_objects` as would return a `__reduce__` method. See the standard library documentation on pickling for more details. Nc�T�tj|||���|�i}ttd��r$tj���|_n#t j���|_|���D]\}}|�||���dS)N)�protocol�dispatch) r�__init__�hasattrr�copy�copyreg�dispatch_table�items�register)�self�writer�reducersr�type� reduce_funcs �[/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/joblib/pool.pyrzCustomizablePickler.__init__Hs������v��9�9�9�9� � ��H� �7�J� '� '� @�$�,�1�1�3�3�D�M�M�#*�"8�"=�"=�"?�"?�D� �!)���!1�!1� -� -� �D�+� �M�M�$� � ,� ,� ,� ,� -� -�c�h��ttd��r�fd�}||j|<dS�|j|<dS)z@Attach a reducer function to a given type in the dispatch table.rc�8���|��}|j|d|i�dS)N�obj)� save_reduce)rr"�reducedrs �r� dispatcherz0CustomizablePickler.register.<locals>.dispatcher\s0���%�+�c�*�*�� �� �7�3�S�3�3�3�3�3rN)rrrr)rrrr%s ` rrzCustomizablePickler.registerWsU��� �7�J� '� '� 4� 4� 4� 4� 4� 4�#-�D�M�$� � � �(3�D� �� %� %� %r)�__name__� __module__� __qualname__�__doc__rrr�rrrr0sI��������.)-�7G� -� -� -� -� 4� 4� 4� 4� 4rrc�2�eZdZdZdd�Zd�Zd�Zd�Zd�ZdS) �CustomizablePicklingQueuea�Locked Pipe implementation that uses a customizable pickler. This class is an alternative to the multiprocessing implementation of SimpleQueue in order to make it possible to pass custom pickling reducers, for instance to avoid memory copy when passing memory mapped datastructures. `reducers` is expected to be a dict with key / values being `(type, callable)` pairs where `callable` is a function that, given an instance of `type`, will return a tuple `(constructor, tuple_of_objects)` to rebuild an instance out of the pickled `tuple_of_objects` as would return a `__reduce__` method. See the standard library documentation on pickling for more details. Nc��||_|�d���\|_|_|���|_t jdkrd|_n|���|_|� ��dS)NF)�duplex�win32) � _reducers�Pipe�_reader�_writer�Lock�_rlock�sys�platform�_wlock� _make_methods)r�contextrs rrz"CustomizablePicklingQueue.__init__usq��!���%,�\�\��\�%?�%?�"�� �d�l��l�l�n�n�� � �<�7� "� "��D�K�K�!�,�,�.�.�D�K� �������rc�`�t|��|j|j|j|j|jfS�N)r r2r3r5r8r0�rs r� __getstate__z&CustomizablePicklingQueue.__getstate__s0�������� �d�l�D�K����� � rc�p�|\|_|_|_|_|_|���dSr<)r2r3r5r8r0r9)r�states r� __setstate__z&CustomizablePicklingQueue.__setstate__�s7�� � ���t�|�T�[�$�+� �� �������rc�6�|j��� Sr<)r2�pollr=s r�emptyzCustomizablePicklingQueue.empty�s���<�$�$�&�&�&�&rc�D���������jjx�_��jj�jjc�����fd�}|�_�jr �fd����_n�j j x�_��j � ��_ dS�j j�j jc�����fd�}|�_ dS)Nc�`����� ������S#���wxYwr<r*)�racquire�recv�rreleases���r�getz4CustomizablePicklingQueue._make_methods.<locals>.get�s9��� �H�J�J�J� ��t�v�v��� � � � ���� � � � ���s� !� -c����t��}t|�j���|���j�|�����dSr<)rrr0�dumpr3� send_bytes�getvalue)r"�bufferrs �r�sendz5CustomizablePicklingQueue._make_methods.<locals>.send�sR��� ����#�F�D�N�;�;�@�@��E�E�E�� �'�'����(9�(9�:�:�:�:�:rc�b����� �|�����S#���wxYwr<r*)r"rP� wlock_acquire� wlock_releases ���r�putz4CustomizablePicklingQueue._make_methods.<locals>.put�s;���� ����$��4��9�9�!�M�O�O�O�O��M�M�O�O�O�O���s� "� .) r2rH�_recvr5�acquire�releaserJr0�_sendr3rPr8rT) rrJrTrGrHrIrPrRrSs ` @@@@@@rr9z'CustomizablePicklingQueue._make_methods�s���������� �L�-�-�� �T�!�[�0�$�+�2E���(� � � � � � � ���� �>� 2� ;� ;� ;� ;� ;��D�J�J� $� � 1� 1�D�J�� �;� ��D�H�H�H�� �#�T�[�%8� )�M�=� $� $� $� $� $� $� $��D�H�H�Hrr<) r&r'r(r)rr>rArDr9r*rrr,r,dsn�������� ���� � � � ��� '�'�'�#�#�#�#�#rr,c�.��eZdZdZ d�fd� Zd�Z�xZS)� PicklingPoola�Pool implementation with customizable pickling reducers. This is useful to control how data is shipped between processes and makes it possible to use shared memory without useless copies induces by the default pickling methods of the original objects passed as arguments to dispatch. `forward_reducers` and `backward_reducers` are expected to be dictionaries with key/values being `(type, callable)` pairs where `callable` is a function that, given an instance of `type`, will return a tuple `(constructor, tuple_of_objects)` to rebuild an instance out of the pickled `tuple_of_objects` as would return a `__reduce__` method. See the standard library documentation about pickling for more details. Nc ����|�t��}|�t��}||_||_t|���}|�|��t t |��jdi|��dS)N)� processesr*)�dict�_forward_reducers�_backward_reducers�update�superrZr)rr\�forward_reducers�backward_reducers�kwargs�poolargs� __class__s �rrzPicklingPool.__init__�s~��� � #�#�v�v� � � $� $��� �!1���"3����)�,�,�,���������*��l�D�!�!�*�6�6�X�6�6�6�6�6rc���t|dt��}t||j��|_t||j��|_|jj|_|jj |_ dS)N�_ctx) �getattrr r,r^�_inqueuer_� _outqueuerX� _quick_putrU� _quick_get)rr:s r� _setup_queueszPicklingPool._setup_queues�se���$���+�+��1�'�26�2H�J�J�� �2�7�37�3J�L�L����-�-����.�.����r)NNN)r&r'r(r)rrn� __classcell__�rfs@rrZrZ�s^��������� 9=�#'� 7� 7� 7� 7� 7� 7�/�/�/�/�/�/�/rrZc�J��eZdZdZ d �fd� Z�fd�Zed ���Z�xZS) �MemmappingPoola>Process pool that shares large arrays to avoid memory copy. This drop-in replacement for `multiprocessing.pool.Pool` makes it possible to work efficiently with shared memory in a numpy context. Existing instances of numpy.memmap are preserved: the child suprocesses will have access to the same shared memory in the original mode except for the 'w+' mode that is automatically transformed as 'r+' to avoid zeroing the original data upon instantiation. Furthermore large arrays from the parent process are automatically dumped to a temporary folder on the filesystem such as child processes to access their content via memmapping (file system backed shared memory). Note: it is important to call the terminate method to collect the temporary folder used by the pool. Parameters ---------- processes: int, optional Number of worker processes running concurrently in the pool. initializer: callable, optional Callable executed on worker process creation. initargs: tuple, optional Arguments passed to the initializer callable. temp_folder: (str, callable) optional If str: Folder to be used by the pool for memmapping large arrays for sharing memory with worker processes. If None, this will try in order: - a folder pointed by the JOBLIB_TEMP_FOLDER environment variable, - /dev/shm if the folder exists and is writable: this is a RAMdisk filesystem available by default on modern Linux distributions, - the default system temporary folder that can be overridden with TMP, TMPDIR or TEMP environment variables, typically /tmp under Unix operating systems. if callable: An callable in charge of dynamically resolving a temporary folder for memmapping large arrays. max_nbytes int or None, optional, 1e6 by default Threshold on the size of arrays passed to the workers that triggers automated memory mapping in temp_folder. Use None to disable memmapping of large arrays. mmap_mode: {'r+', 'r', 'w+', 'c'} Memmapping mode for numpy arrays passed to workers. See 'max_nbytes' parameter documentation for more details. forward_reducers: dictionary, optional Reducers used to pickle objects passed from main process to worker processes: see below. backward_reducers: dictionary, optional Reducers used to pickle return values from workers back to the main process. verbose: int, optional Make it possible to monitor how the communication of numpy arrays with the subprocess is handled (pickling or memmapping) prewarm: bool or str, optional, "auto" by default. If True, force a read on newly memmapped array to make sure that OS pre-cache it in memory. This can be useful to avoid concurrent disk access when the same data array is passed to different worker processes. If "auto" (by default), prewarm is set to True, unless the Linux shared memory partition /dev/shm is available and used as temp folder. `forward_reducers` and `backward_reducers` are expected to be dictionaries with key/values being `(type, callable)` pairs where `callable` is a function that give an instance of `type` will return a tuple `(constructor, tuple_of_objects)` to rebuild an instance out of the pickled `tuple_of_objects` as would return a `__reduce__` method. See the standard library documentation on pickling for more details. N���.A�rrFc �8��|�tjdt��t|��} | |_t | j|||||d| ���\}}t|||���} | �| ��tt|��j di| ��dS)NzPcontext_id is deprecated and ignored in joblib 0.9.4 and will be removed in 0.11F)�temp_folder_resolver� max_nbytes� mmap_moderbrc�verbose�unlink_on_gc_collect�prewarm)r\rbrcr*) �warnings�warn�DeprecationWarningr �_temp_folder_managerr�resolve_temp_folder_namer]r`rarrr)rr\� temp_folderrwrxrbrcry� context_idr{rd�managerrerfs �rrzMemmappingPool.__init__&s���� � !� �M�?�,� .� .� .�,�K�8�8��$+��!� $�%,�%E�%��!1�"3�W�%*�G�  =� =� =� ,��+���-�/�1�1�1�� �������,��n�d�#�#�,�8�8�x�8�8�8�8�8rc�j��d}t|��D]�} tt|�����n[#t$rN}t |t ��r/td��|dz|krtj d|z��Yd}~�d}~wwxYw|j � ��dS)N� g�������?rz@Failed to terminate worker processes in multiprocessing pool: %r) �rangerarr� terminate�OSError� isinstance� WindowsErrorrr|r}r�_clean_temporary_resources)r� n_retries�i�erfs �rr�zMemmappingPool.terminateEs����� ��y�!�!� G� G�A� G��n�d�+�+�5�5�7�7�7����� G� G� G��a��.�.�G��#�J�J�J��1�u� �)�)� � �'B�DE�'F�G�G�G����������  G���� �!�<�<�>�>�>�>�>s�'?� B� AB�Bc�|�t|dd���|jS|j���|_|jS)N�_cached_temp_folder)rir�rr�r=s r� _temp_folderzMemmappingPool._temp_folderWs@�� �4�.�� 5� 5� A��+� +�'+�'@�'Y�'Y�'[�'[�D� $��+� +r) NNrsrtNNrNF) r&r'r(r)rr��propertyr�rorps@rrrrr�s��������J�J�XEH�IM�5:�9�9�9�9�9�9�>?�?�?�?�?�$� ,� ,��X� ,� ,� ,� ,� ,rrr)r)rr6r|�timerr�� NameErrorr�picklerr�ior�_memmapping_reducerrr �_multiprocessing_helpersr r �multiprocessing.poolr �numpy�np� ImportErrorr�objectr,rZrrr*rr�<module>r�s��� � ����� � � � �������������L�L�������4��:�:�L�L�L�����������#�#�#�#�#�#�������8�8�8�8�8�8�:�:�:�:�:�:�9�9�9�9�9�9�9�9�&�%�%�%�%�%������������ �B�B�B�����14�14�14�14�14�'�14�14�14�hK�K�K�K�K��K�K�K�\$/�$/�$/�$/�$/�4�$/�$/�$/�NI,�I,�I,�I,�I,�\�I,�I,�I,�I,�I,s��,�,�A!�!A+�*A+
Memory