Edit File: shared_memory.cpython-38.pyc
U ����@��dD����������������������@���s����d�Z�ddgZddlmZ�ddlZddlZddlZddlZddlZej dkrXddl Z dZnddlZdZej ejB�Zd Zer~d ZndZdd ��ZG�dd��d�ZdZG�dd��d�ZdS�)z�Provides shared memory for direct access across processes. The API of this package is currently provisional. Refer to the documentation for details. �SharedMemory� ShareableList�����)�partialN�ntFT����z/psm_Zwnsm_c������������������C���sB���t�tt��d�}�|�dks td��tt�|���}t|�t�ks>t�|S�)z6Create a random filename for the shared memory object.����z_SHM_NAME_PREFIX too long)�_SHM_SAFE_NAME_LENGTH�len�_SHM_NAME_PREFIX�AssertionError�secretsZ token_hex)�nbytes�name��r����B/opt/alt/python38/lib64/python3.8/multiprocessing/shared_memory.py�_make_filename&���s ����r���c�������������������@���s����e�Zd�ZdZdZdZdZdZej Z dZer.dndZ ddd �Zd d��Zdd ��Zdd��Zedd���Zedd���Zedd���Zdd��Zdd��ZdS�)r���a���Creates a new shared memory block or attaches to an existing shared memory block. Every shared memory block is assigned a unique name. This enables one process to create a shared memory block with a particular name so that a different process can attach to that same shared memory block using that same name. As a resource for sharing data across processes, shared memory blocks may outlive the original process that created them. When one process no longer needs access to a shared memory block that might still be needed by other processes, the close() method should be called. When a shared memory block is no longer needed by any process, the unlink() method should be called to ensure proper cleanup.N���i���TFr���c����������� ��� ���C���sl��|dkst�d��|r0ttjB�|�_|dkr0t�d��|d�krL|�jtj@�sLt�d��t�rH|d�kr�t��}ztj ||�j|�j d�|�_W�n�tk r����Y�qZY�nX�||�_ q�qZn.|�jr�d|�n|}tj ||�j|�j d�|�_||�_ z<|r�|r�t�|�j|��t�|�j�}|j}t�|�j|�|�_W�n �tk �r*���|�������Y�nX�ddlm}�||�j d ���n|�r�|d�k�r^t��n|}t�tjtjtj|d ?�d@�|d@�|�}zXt���}|tjk�r�|d�k �r�tt j!t�"t j!�|tj��nW���qNtjd||d �|�_W�5�t�|��X�||�_ �qV�qNnX||�_ t�#tj$d|�}zt�%|tj$ddd�} W�5�t�|��X�t�&| �}tjd||d �|�_||�_'t(|�j�|�_)d�S�)Nr���z!'size' must be a positive integerz4'size' must be a positive number different from zeroz&'name' can only be None if create=True)�mode�/����)�register� shared_memory� ���l������r���)ZtagnameF)*� ValueError�_O_CREX�os�O_RDWR�_flags�O_EXCL� _USE_POSIXr����_posixshmemZshm_open�_mode�_fd�FileExistsError�_name�_prepend_leading_slash� ftruncate�fstat�st_size�mmap�_mmap�OSError�unlink�resource_trackerr����_winapiZCreateFileMappingZINVALID_HANDLE_VALUEZNULLZPAGE_READWRITEZCloseHandleZGetLastErrorZERROR_ALREADY_EXISTS�errnoZEEXIST�strerrorZOpenFileMappingZ FILE_MAP_READZ MapViewOfFileZVirtualQuerySize�_size� memoryview�_buf) �selfr����create�sizeZstatsr���Z temp_nameZh_mapZlast_error_codeZp_bufr���r���r����__init__I���s������ � � � �� zSharedMemory.__init__c�����������������C���s&���z|������W�n�tk r ���Y�nX�d�S��N)�closer+����r4���r���r���r����__del__����s����zSharedMemory.__del__c�����������������C���s���|�j�|�jd|�jffS�)NF)� __class__r���r6���r:���r���r���r���� __reduce__����s������zSharedMemory.__reduce__c�����������������C���s���|�j�j��d|�j�d|�j��d�S�)N�(z, size=�))r<����__name__r���r6���r:���r���r���r����__repr__����s����zSharedMemory.__repr__c�����������������C���s���|�j�S�)z4A memoryview of contents of the shared memory block.)r3���r:���r���r���r����buf����s����zSharedMemory.bufc�����������������C���s.���|�j�}tr*|�jr*|�j��d�r*|�j�dd��}|S�)z4Unique name that identifies the shared memory block.r���r���N)r$���r���r%���� startswith)r4���Z reported_namer���r���r���r�������s ���� zSharedMemory.namec�����������������C���s���|�j�S�)zSize in bytes.)r1���r:���r���r���r���r6�������s����zSharedMemory.sizec�����������������C���sX���|�j�dk r|�j�����d|�_�|�jdk r4|�j����d|�_trT|�jdkrTt�|�j��d|�_dS�)zkCloses access to the shared memory from this instance but does not destroy the shared memory block.Nr���r���)r3����releaser*���r9���r���r"���r���r:���r���r���r���r9�������s���� zSharedMemory.closec�����������������C���s2���t�r.|�jr.ddlm}�t�|�j��||�jd��dS�)z�Requests that the underlying shared memory block be destroyed. In order to ensure proper cleanup of resources, unlink should be called once (and only once) across all processes which have access to the shared memory block.r���)� unregisterr���N)r���r$���r-���rE���r ���Z shm_unlink)r4���rE���r���r���r���r,�������s���� zSharedMemory.unlink)NFr���)r@���� __module__�__qualname__�__doc__r$���r"���r*���r3���r���r���r���r!���r���r%���r7���r;���r=���rA����propertyrB���r���r6���r9���r,���r���r���r���r���r���0���s(��� l �utf8c�������������������@���s��e�Zd�ZdZedededededdj diZ dZd d ��dd ��dd ��d d ��d�Ze dd���Zd6dd�dd�Zdd��Zdd��Zdd��Zdd��Zdd��Zdd��Zd d!��Zd"d#��Zed$d%���Zed&d'���Zed(d)���Zed*d+���Zed,d-���Zed.d/���Zed0d1���Zd2d3��Z d4d5��Z!dS�)7r���a���Pattern for a mutable list-like object shareable via a shared memory block. It differs from the built-in list type in that these lists can not change their overall length (i.e. no append, insert, etc.) Because values are packed into a memoryview as bytes, the struct packing format for any storable value must require no more than 8 characters to describe its format.�q�dzxxxxxxx?z%dsNzxxxxxx?x����c�����������������C���s���|�S�r8���r�����valuer���r���r����<lambda> �������zShareableList.<lambda>c�����������������C���s���|���d��t�S��N�����)�rstrip�decode� _encodingrN���r���r���r���rP�����rQ���c�����������������C���s ���|���d�S�rR���)rT���rN���r���r���r���rP�����rQ���c�����������������C���s���d�S�r8���r���)Z_valuer���r���r���rP��� ��rQ���)r���r���r�������c�����������������C���s:���t�|�ttdjf�sdS�t�|�t�r$dS�t�|�t�r2dS�dS�dS�)z�Used in concert with _back_transforms_mapping to convert values into the appropriate Python objects when retrieving them from the list as well as when storing them.Nr���r���r���rW���)� isinstance�str�bytesr<���rN���r���r���r����_extract_recreation_code��s���� z&ShareableList._extract_recreation_code�r���c�������������������s���|d�k r��fdd�|D��}t�|��_tdd��|D����jks@t�t�fdd�|D����_�fdd�|D��}t�d�j�d� |���j ��j��}nd }|d�k r�|d�kr�t|��_ nt|d |d��_ |d�k �rjt��tjd�j��j jd�jf�j����tjd� |��j j�jf��fd d�|D������tj�j �j j�jf��fdd�|D������tj�j�j j�jf|����n t����_t��j�j jd ��_d�S�)Nc��������������������sP���g�|�]H}t�|ttf�s$��jt|��n&��jt|����jt|���j�d���f��qS�)r���)rX���rY���rZ����_types_mapping�type� _alignmentr �����.0�itemr:���r���r���� <listcomp> ��s������z*ShareableList.__init__.<locals>.<listcomp>c�����������������s���s���|�]}t�|�d�kV��qdS�)rM���N)r ����ra����fmtr���r���r���� <genexpr>)��s�����z)ShareableList.__init__.<locals>.<genexpr>c�����������������3���s0���|�](}|d��dkr��j�nt|dd����V��qdS�)r����sN)r_����intrd���r:���r���r���rf���*��s����c��������������������s���g�|�]}����|��qS�r���)r[���r`���r:���r���r���rc���.��s����rK�����rM���T)r5���r6���r���c�����������������3���s&���|�]}t�|t�r|����n|V��qd�S�r8���)rX���rY����encode�ra����v��_encr���r���rf���M��s�����c�����������������3���s���|�]}|�����V��qd�S�r8���)rj���rk���rm���r���r���rf���S��s�����)r ���� _list_len�sumr����tuple�_allocated_bytes�structZcalcsize�_format_size_metainfo�join�_format_packing_metainfo�_format_back_transform_codesr����shmrV���� pack_intorB����_offset_data_start�_offset_packing_formats�_offset_back_transform_codes�unpack_from)r4���Zsequencer���Z_formatsZ_recreation_codesZrequested_sizer���)rn���r4���r���r7�����s|���� � � ����� �������� �zShareableList.__init__c�����������������C���sj���|dkr|n||�j��}||�j�ks*|�j�dk�r2td��t�d|�jj|�j|d���d�}|�d�}|�t �}|S�)z>Gets the packing format for a single value stored in the list.r���� Requested position out of range.�8srM���rS���) ro���� IndexErrorrs���r}���rx���rB���r{���rT���rU���rV���)r4����positionrl���re���� fmt_as_strr���r���r����_get_packing_formatd��s������ z!ShareableList._get_packing_formatc�����������������C���s\���|dkr|n||�j��}||�j�ks*|�j�dk�r2td��t�d|�jj|�j|��d�}|�j|�}|S�)z9Gets the back transformation function for a single value.r���r~����b)ro���r����rs���r}���rx���rB���r|����_back_transforms_mapping)r4���r�����transform_codeZtransform_functionr���r���r����_get_back_transformt��s������ z!ShareableList._get_back_transformc�����������������C���s~���|dkr|n||�j��}||�j�ks*|�j�dk�r2td��t�d|�jj|�j|d��|�t���|�� |�}t�d|�jj|�j |�|��dS�)zvSets the packing format and back transformation code for a single value in the list at the specified position.r���r~���r���rM���r����N)ro���r����rs���ry���rx���rB���r{���rj���rV���r[���r|���)r4���r����r����rO���r����r���r���r����!_set_packing_format_and_transform���s ����� �z/ShareableList._set_packing_format_and_transformc�����������������C���sj���z6|�j�t|�jd�|����}t�|��|�|�jj|�\}W�n�tk rR���td��Y�nX�|�� |�}||�}|S�)Nzindex out of range) rz���rp���rr���rs���r}���r����rx���rB���r����r����)r4���r�����offsetrl���Zback_transformr���r���r����__getitem__���s������ zShareableList.__getitem__c�����������������C���s����z&|�j�t|�jd�|����}|��|�}W�n�tk rB���td��Y�nX�t|ttf�sf|�jt |��}|}nZt|t�rz|� t�n|}t|�|�j|�kr�t d��|d�dkr�|}n|�jt�|�j|�f�}|��|||��t�||�jj||��d�S�)Nzassignment index out of rangez(bytes/str item exceeds available storager���rg���)rz���rp���rr���r����r����rX���rY���rZ���r]���r^���rj���rV���r ���r���r����rs���ry���rx���rB���)r4���r����rO���r����Zcurrent_formatZ new_formatZ encoded_valuer���r���r����__setitem__���s6���������zShareableList.__setitem__c�����������������C���s���t�|�j|�jjd�dfS�)Nr\���r���)r���r<���rx���r���r:���r���r���r���r=������s����zShareableList.__reduce__c�����������������C���s���t��d|�jjd�d�S�)NrK���r���)rs���r}���rx���rB���r:���r���r���r����__len__���s����zShareableList.__len__c�����������������C���s"���|�j�j��dt|����d|�jj�d�S�)Nr>���z, name=r?���)r<���r@����listrx���r���r:���r���r���r���rA������s����zShareableList.__repr__c��������������������s���d����fdd�t��j�D���S�)z>The struct packing format used by all currently stored values.ri���c�����������������3���s���|�]}����|�V��qd�S�r8���)r����)ra����ir:���r���r���rf������s����z'ShareableList.format.<locals>.<genexpr>)ru����rangero���r:���r���r:���r����format���s�����zShareableList.formatc�����������������C���s���|�j���d�S�)z=The struct packing format used for metainfo on storage sizes.rK����ro���r:���r���r���r���rt������s����z#ShareableList._format_size_metainfoc�����������������C���s ���d|�j��S�)z?The struct packing format used for the values' packing formats.r���r����r:���r���r���r���rv������s����z&ShareableList._format_packing_metainfoc�����������������C���s ���d|�j��S�)z?The struct packing format used for the values' back transforms.r����r����r:���r���r���r���rw������s����z*ShareableList._format_back_transform_codesc�����������������C���s���|�j�d�d�S�)Nr���rM���r����r:���r���r���r���rz������s����z ShareableList._offset_data_startc�����������������C���s���|�j�t|�j��S�r8���)rz���rp���rr���r:���r���r���r���r{������s����z%ShareableList._offset_packing_formatsc�����������������C���s���|�j�|�jd��S�)NrM���)r{���ro���r:���r���r���r���r|������s����z*ShareableList._offset_back_transform_codesc��������������������s���t���fdd�|�D���S�)zCL.count(value) -> integer -- return number of occurrences of value.c�����������������3���s���|�]}��|kV��qd�S�r8���r���)ra����entryrN���r���r���rf������s�����z&ShareableList.count.<locals>.<genexpr>)rp���)r4���rO���r���rN���r����count���s����zShareableList.countc�����������������C���s4���t�|��D�]\}}||kr|��S�qt|�d���dS�)zpL.index(value) -> integer -- return first index of value. Raises ValueError if the value is not present.z not in this containerN)� enumerater���)r4���rO���r����r����r���r���r����index���s���� zShareableList.index)N)"r@���rF���rG���rH���rh����float�boolrY���rZ���r<���r]���r_���r�����staticmethodr[���r7���r����r����r����r����r����r=���r����rA���rI���r����rt���rv���rw���rz���r{���r|���r����r����r���r���r���r���r�������s^��� �������� F )rH����__all__� functoolsr���r)���r���r/���rs���r���r���r.���r���r ����O_CREATr���r���r���r ���r���r���rV���r���r���r���r���r����<module>���s,��� �E