o ?OgH@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_Zwnsm_cCs"tttd}tt|}|S)z6Create a random filename for the shared memory object.)_SHM_SAFE_NAME_LENGTHlen_SHM_NAME_PREFIXsecretsZ token_hex)nbytesnamerD/opt/alt/python310/lib64/python3.10/multiprocessing/shared_memory.py_make_filename(src@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)ZtagnameF)+ ValueError_O_CREXosO_RDWR_flagsO_EXCL _USE_POSIXr _posixshmemZshm_open_mode_fdFileExistsError_name_prepend_leading_slash ftruncatefstatst_sizemmap_mmapOSErrorunlinkrregister_winapiZCreateFileMappingZINVALID_HANDLE_VALUEZNULLZPAGE_READWRITEZ GetLastErrorZERROR_ALREADY_EXISTSerrnoZEEXISTstrerrorZ CloseHandleZOpenFileMappingZ FILE_MAP_READZ MapViewOfFileZVirtualQuerySizeZUnmapViewOfFile_size memoryview_buf) selfrcreatesizeZstatsZ temp_nameZh_mapZlast_error_codeZp_bufrrr__init__Ks         zSharedMemory.__init__cCs$z|WdStyYdSwN)closer*r3rrr__del__s  zSharedMemory.__del__cCs|j|jd|jffS)NF) __class__rr5r9rrr __reduce__s zSharedMemory.__reduce__cCs|jjd|jd|jdS)N(z, size=))r;__name__rr5r9rrr__repr__szSharedMemory.__repr__cC|jS)z4A memoryview of contents of the shared memory block.)r2r9rrrbufzSharedMemory.bufcCs.|j}tr|jr|jdr|jdd}|S)z4Unique name that identifies the shared memory block.rrN)r#rr$ startswith)r3Z reported_namerrrrs   zSharedMemory.namecCrA)zSize in bytes.)r0r9rrrr5rCzSharedMemory.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)r2releaser)r8rr!rr9rrrr8s      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)rr#rZ shm_unlinkr unregisterr9rrrr+s  zSharedMemory.unlink)NFr)r? __module__ __qualname____doc__r#r!r)r2rrrr rr$r6r:r<r@propertyrBrr5r8r+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|Sr7rvaluerrrzShareableList.cCs|dtSN)rstripdecode _encodingrOrrrrQscCs |dSrS)rUrOrrrrQs cCsdSr7r)Z_valuerrrrQrR)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 rX) isinstancestrbytesr;rOrrr_extract_recreation_codes  z&ShareableList._extract_recreation_codercs|dus|durc|p d}fdd|D}t|_d}dg_|D]}||ddkr.jnt|dd7}j|q"fdd|D}tdjd |j j }t |d |d _ nt |_ |durttjdjj jdjgjRtjd |j jjgfd d |DRtjj j jjgfdd |DRtjj j jjg|RdSt_ttjj jd_dS)NrcsPg|]$}t|ttfsjt|njt|jt|jdfqS)r)rYrZr[_types_mappingtype _alignmentr .0itemr9rr -s  z*ShareableList.__init__..rrscsg|]}|qSr)r\rar9rrrd?s rLT)r4r5c3s(|]}t|tr|n|VqdSr7)rYrZencoderbv_encrr Zs&z)ShareableList.__init__..c3s|]}|VqdSr7)rgrhrjrrrl`srN)r _list_len_allocated_offsetsr`intappendstructZcalcsize_format_size_metainfojoin_format_packing_metainfo_format_back_transform_codesrshmrW pack_intorB_offset_data_start_offset_packing_formats_offset_back_transform_codeslist unpack_from)r3ZsequencerZ_formatsoffsetfmtZ_recreation_codesZrequested_sizer)rkr3rr6*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.8srNrT) rm IndexErrorrqr|rvrBryrUrVrW)r3positionrir~ 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)rmrrqr|rvrBrz_back_transforms_mapping)r3rtransform_codeZtransform_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.rrrrNrN) rmrrqrwrvrBryrgrWr\rz)r3rrrPrrrr!_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) rmrxrnrqr|rrvrBrr)r3rr}riZback_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 storagerre)rmrnrxrrrYrZr[r^r_rgrWr rrrqrwrvrB) r3rrPZ item_offsetr}Zcurrent_formatZ new_formatZ encoded_valueZallocated_lengthrrr __setitem__s:      zShareableList.__setitem__cCst|j|jjddfS)Nr]r)rr;rvrr9rrrr<zShareableList.__reduce__cCstd|jjddS)NrLr)rqr|rvrBr9rrr__len__rzShareableList.__len__cCs"|jjdt|d|jjdS)Nr=z, name=r>)r;r?r{rvrr9rrrr@s"zShareableList.__repr__csdfddtjDS)z=The struct packing format used by all currently stored items.rfc3s|]}|VqdSr7)r)rbir9rrrls  z'ShareableList.format..)rsrangermr9rr9rformatszShareableList.formatcCsd|jdS)z>The struct packing format used for the items' storage offsets.rLrrmr9rrrrrsz#ShareableList._format_size_metainfocC d|jS)z>The struct packing format used for the items' packing formats.rrr9rrrrt z&ShareableList._format_packing_metainfocCr)z>The struct packing format used for the items' back transforms.rrr9rrrrurz*ShareableList._format_back_transform_codescCs|jddS)Nr rNrr9rrrrxsz ShareableList._offset_data_startcCs|j|jdS)Nr)rxrnr9rrrryz%ShareableList._offset_packing_formatscCs|j|jdS)NrN)ryrmr9rrrrzrz*ShareableList._offset_back_transform_codescstfdd|DS)zCL.count(value) -> integer -- return number of occurrences of value.c3s|]}|kVqdSr7r)rbentryrOrrrl sz&ShareableList.count..)sum)r3rPrrOrcountszShareableList.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)r3rPrrrrrindex s zShareableList.indexr7)&r?rGrHrIrofloatboolrZr[r;r^r`r staticmethodr\r6rrrrrr<rr@rJrrrrtrurxryrzrr classmethodtypes GenericAlias__class_getitem__rrrrrsV  I!        )rI__all__ functoolsrr(rr.rqr rrr-rrrfrO_CREATrrr r rrrWrrrrrs2     F