o [dH@sdZddgZddlmZddlZddlZddlZddlZddlZddl Z ej dkr0ddl Z dZ nddl Z dZ d d lmZejejBZd Ze rId Znd ZddZGdddZdZGdddZdS)zProvides shared memory for direct access across processes. The API of this package is currently provisional. Refer to the documentation for details. SharedMemory ShareableList)partialNntFT)resource_trackerz/psm_wnsm_cCsBtttd}|dksJdtt|}t|tksJ|S)z6Create a random filename for the shared memory object.z_SHM_NAME_PREFIX too long)_SHM_SAFE_NAME_LENGTHlen_SHM_NAME_PREFIXsecrets token_hex)nbytesnamer4/usr/lib/python3.10/multiprocessing/shared_memory.py_make_filename(s rc@seZdZdZdZdZdZdZej Z dZ e rdndZ ddd Zd d Zd d ZddZeddZeddZeddZddZddZdS)raCreates 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.NiTFrc Csx|dkstd|rttjB|_|dkrtd|dur&|jtj@s&tdtr|durL t}z tj ||j|j d|_ Wn t yFYq,w||_ n|jrSd|n|}tj ||j|j d|_ ||_ z|ro|rot|j |t|j }|j}t|j ||_Wn ty|wt|j dn|r |durtn|}ttjtjtj|d ?d @|d @|}z1t}|tjkr|durt tj t!tj |tjWt"|qtjd ||d |_Wt"|nt"|w||_ n?||_ t#tj$d |}zt%|tj$ddd}Wt"|nt"|wz t&|}Wt'|nt'|wtjd ||d |_||_(t)|j|_*dS)Nrz!'size' must be a positive integerz4'size' must be a positive number different from zeroz&'name' can only be None if create=TrueT)mode/ shared_memory lr)tagnameF)+ ValueError_O_CREXosO_RDWR_flagsO_EXCL _USE_POSIXr _posixshmemshm_open_mode_fdFileExistsError_name_prepend_leading_slash ftruncatefstatst_sizemmap_mmapOSErrorunlinkrregister_winapiCreateFileMappingINVALID_HANDLE_VALUENULLPAGE_READWRITE GetLastErrorERROR_ALREADY_EXISTSerrnoEEXISTstrerror CloseHandleOpenFileMapping FILE_MAP_READ MapViewOfFileVirtualQuerySizeUnmapViewOfFile_size memoryview_buf) selfrcreatesizestats temp_nameh_maplast_error_codep_bufrrr__init__Ks         zSharedMemory.__init__cCs$z|WdStyYdSwN)closer.rDrrr__del__s  zSharedMemory.__del__cCs|j|jd|jffS)NF) __class__rrFrOrrr __reduce__s zSharedMemory.__reduce__cCs|jjd|jd|jdS)N(z, size=))rQ__name__rrFrOrrr__repr__szSharedMemory.__repr__cC|jS)z4A memoryview of contents of the shared memory block.)rCrOrrrbufzSharedMemory.bufcCs.|j}tr|jr|jdr|jdd}|S)z4Unique name that identifies the shared memory block.rrN)r'r!r( startswith)rD reported_namerrrrs   zSharedMemory.namecCrW)zSize in bytes.)rArOrrrrFrYzSharedMemory.sizecCs`|jdur |jd|_|jdur|jd|_tr,|jdkr.t|jd|_dSdSdS)zkCloses access to the shared memory from this instance but does not destroy the shared memory block.Nrr)rCreleaser-rNr!r%rrOrrrrNs      zSharedMemory.closecCs0tr|jrt|jt|jddSdSdS)zRequests 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.rN)r!r'r" shm_unlinkr unregisterrOrrrr/s  zSharedMemory.unlink)NFr)rU __module__ __qualname____doc__r'r%r-rCrrrr$r!r(rLrPrRrVpropertyrXrrFrNr/rrrrr2s*  n     utf8c @seZdZdZedededededdj diZ dZ d d d d d d d d dZ e ddZd6ddddZddZddZddZddZddZddZd d!Zd"d#Zed$d%Zed&d'Zed(d)Zed*d+Zed,d-Zed.d/Zed0d1Zd2d3Z d4d5Z!e"e#j$Z%dS)7raPattern 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.qdzxxxxxxx?z%dsNzxxxxxx?xcCs|SrMrvaluerrrzShareableList.cCs|dtSN)rstripdecode _encodingrgrrrriscCs |dSrk)rmrgrrrris cCsdSrMr)_valuerrrrirj)rrr cCs6t|ttdjfs dSt|trdSt|trdSdS)zUsed 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.Nrrr rq) isinstancestrbytesrQrgrrr_extract_recreation_codes  z&ShareableList._extract_recreation_codercs|dus|durq|p d}fdd|D}t|_tdd|Djks(Jd}dg_|D]}||ddkr<jnt|dd7}j|q0fd d|D}td j d |j j }t |d |d _nt |_|durttjd j jjdjgjRtjd |jjjgfdd|DRtjj jjjgfdd|DRtjj jjjg|RdSt_ttj jjd_dS)NrcsPg|]$}t|ttfsjt|njt|jt|jdfqS)r)rrrsrt_types_mappingtype _alignmentr .0itemrOrr -s  z*ShareableList.__init__..css|] }t|dkVqdS)rfN)r )r{fmtrrr 6sz)ShareableList.__init__..rrscsg|]}|qSr)rurzrOrrr}?s rdT)rErFc3s(|]}t|tr|n|VqdSrM)rrrsencoder{v_encrrrZs&c3s|]}|VqdSrM)rrrrrr`srf)r _list_lensum_allocated_offsetsryintappendstructcalcsize_format_size_metainfojoin_format_packing_metainfo_format_back_transform_codesrshmro pack_intorX_offset_data_start_offset_packing_formats_offset_back_transform_codeslist unpack_from)rDsequencer_formatsoffsetr~_recreation_codesrequested_sizer)rrDrrL*s  &    zShareableList.__init__cCsj|dkr|n||j}||jks|jdkrtdtd|jj|j|dd}|d}|t }|S)z>Gets the packing format for a single value stored in the list.r Requested position out of range.8srfrl) r IndexErrorrrrrXrrmrnro)rDpositionrr~ fmt_as_strrrr_get_packing_formatss   z!ShareableList._get_packing_formatcCsF||jks |jdkrtdtd|jj|j|d}|j|}|S)z9Gets the back transformation function for a single value.rrb)rrrrrrXr_back_transforms_mapping)rDrtransform_codetransform_functionrrr_get_back_transforms z!ShareableList._get_back_transformcCsh||jks |jdkrtdtd|jj|j|d|t| |}td|jj|j ||dS)zvSets the packing format and back transformation code for a single value in the list at the specified position.rrrrfrN) rrrrrrXrrrorur)rDrrrhrrrr!_set_packing_format_and_transforms  z/ShareableList._set_packing_format_and_transformcCsr|dkr|n||j}z|j|j|}t|||jj|\}Wn ty-tdw| |}||}|S)Nrzindex out of range) rrrrrrrrXrr)rDrrrback_transformrrr __getitem__s  zShareableList.__getitem__c Cs|dkr|n||j}z|j|}|j|}||}Wn ty'tdwt|ttfs9|jt |}|}n0|j|d|}t|trL| t n|}t ||krXt d|ddkra|}n|jt|f}||||t||jj||dS)Nrzassignment index out of rangerz(bytes/str item exceeds available storagerr)rrrrrrrrsrtrwrxrror rrrrrrX) rDrrh item_offsetrcurrent_format new_format encoded_valueallocated_lengthrrr __setitem__s:      zShareableList.__setitem__cCst|j|jjddfS)Nrvr)rrQrrrOrrrrRzShareableList.__reduce__cCstd|jjddS)Nrdr)rrrrXrOrrr__len__rzShareableList.__len__cCs"|jjdt|d|jjdS)NrSz, name=rT)rQrUrrrrOrrrrVs"zShareableList.__repr__csdfddtjDS)z=The struct packing format used by all currently stored items.rc3s|]}|VqdSrM)r)r{irOrrrs  z'ShareableList.format..)rrangerrOrrOrformatszShareableList.formatcCsd|jdS)z>The struct packing format used for the items' storage offsets.rdrrrOrrrrsz#ShareableList._format_size_metainfocC d|jS)z>The struct packing format used for the items' packing formats.rrrOrrrr z&ShareableList._format_packing_metainfocCr)z>The struct packing format used for the items' back transforms.rrrOrrrrrz*ShareableList._format_back_transform_codescCs|jddS)Nr rfrrOrrrrsz ShareableList._offset_data_startcCs|j|jdS)Nr)rrrOrrrrz%ShareableList._offset_packing_formatscCs|j|jdS)Nrf)rrrOrrrrrz*ShareableList._offset_back_transform_codescstfdd|DS)zCL.count(value) -> integer -- return number of occurrences of value.c3s|]}|kVqdSrMr)r{entryrgrrr sz&ShareableList.count..)r)rDrhrrgrcountszShareableList.countcCs0t|D] \}}||kr|Sqt|d)zpL.index(value) -> integer -- return first index of value. Raises ValueError if the value is not present.z not in this container) enumerater)rDrhrrrrrindex s zShareableList.indexrM)&rUr_r`rarfloatboolrsrtrQrwryr staticmethodrurLrrrrrrRrrVrbrrrrrrrrr classmethodtypes GenericAlias__class_getitem__rrrrrsV  I!        )ra__all__ functoolsrr,rr8rrrrr1r!r"rrO_CREATr rr r rrrorrrrrs2     F