gHdZddgZddlmZddlZddlZddlZddlZddlZddl Z ejdk(rddl Z dZ nddl Z dZ d d lmZej ej"zZd Ze rd Znd ZdZGddZdZGddZy)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_cptttz dz}ttj|z}|S)z6Create a random filename for the shared memory object.)_SHM_SAFE_NAME_LENGTHlen_SHM_NAME_PREFIXsecrets token_hex)nbytesnames D/opt/alt/python312/lib64/python3.12/multiprocessing/shared_memory.py_make_filenamer(s3$c*:&;; AF g//7 7D KceZdZdZdZdZdZdZejZ dZ e rdndZ ddZdZd Zd Zed Zed Zed ZdZdZy)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.NiTFc|dk\s td|r,ttjz|_|dk(r td|(|jtj zs tdt r)|E t} tj||j|j|_ ||_ nK|jrd|zn|}tj||j|j|_ ||_ |r"|r tj|j|tj |j}|j"}t%j$|j||_t-j.|jdn|r" | tn|}t1j2t0j4t0j6t0j8|dz d z|d z|} t1j:}|t0j<k(rd|Ktt>j@tjBt>j@|t0j< t1jD|t%j$d || |_t1jD|||_ n||_ t1jFt0jHd |} t1jJ|t0jHddd}t1jD| t1jL|}t1jN|t%j$d || |_||_(tS|j&|_*y#t$rY+wxYw#t($r|j+wxYw#t1jD|wxYw#t1jD|wxYw#t1jN|wxYw) Nrz!'size' must be a positive integerz4'size' must be a positive number different from zeroz&'name' can only be None if create=True)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_bufs r__init__zSharedMemory.__init__KsOqy@A A !BII-DKqy !WXX < bii 7EF F |)+D!#.#7#7  KK!%$"&DJ%)%@%@sTzd&//KK "  dLL40*}}!YYtxx6  % %djj/ B 48L 0dI$5544 ..z1z)! E3*1*>*>*@*g.J.JJ#/&5$)LL$&KK $=$($+$@$@ '"!"!) ++E2&*YYr4%K ++E2!*DJ"  //)) /#11-- E''.3"33E:D++E2!YYr4>  tzz* k+! !$   J ++E2,''.++E2sJ>1NA4N!9A4N?N?+'O(O3 NN!N<?OO03P cD |jy#t$rYywxYwN)closer2rHs r__del__zSharedMemory.__del__s"  JJL   s  cL|j|jd|jffS)NF) __class__rrJrTs r __reduce__zSharedMemory.__reduce__s) NN     rch|jjd|jd|jdS)N(z, size=))rW__name__rrJrTs r__repr__zSharedMemory.__repr__s...))*!DII= {!LLrc|jS)z4A memoryview of contents of the shared memory block.)rGrTs rbufzSharedMemory.bufsyyrc|j}tr6|jr*|jjdr|jdd}|S)z4Unique name that identifies the shared memory block.rrN)r+r%r, startswith)rH reported_names rrzSharedMemory.names? $55zz$$S) $ 12 rc|jS)zSize in bytes.)rErTs rrJzSharedMemory.sizeszzrc2|j!|jjd|_|j!|jjd|_tr7|j dk\r't j|j d|_yyy)zkCloses access to the shared memory from this instance but does not destroy the shared memory block.Nrr)rGreleaser1rSr%r)r!rTs rrSzSharedMemory.closesq 99 II   DI :: ! JJ   DJ $((a- HHTXX DH(:rctrM|jr@tj|jt j |jdyyy)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 unregisterrTs rr3zSharedMemory.unlinks: $**  " "4:: .  ' ' O D%:r)NFr)r\ __module__ __qualname____doc__r+r)r1rGr!r"r#r(r%r,rPrUrXr]propertyr_rrJrSr3rrrr2s B E C E D YYF E%/TUl+\  M Erutf8c HeZdZdZedededededdjdiZ dZ d d d d d Z e dZd"dddZdZdZdZdZdZdZdZdZedZedZedZedZedZedZedZd Z d!Z!e"e#jHZ%y)#raPattern 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?xc|SrRrmvalues rzShareableList.srcJ|jdjtSN)rstripdecode _encodingrts rrvzShareableList.sg.55i@rc$|jdSrx)rzrts rrvzShareableList.s g.rcyrRrm)_values rrvzShareableList.s$r)rrr ct|ttdjfsyt|tryt|tryy)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 r) isinstancestrbytesrWrts r_extract_recreation_codez&ShareableList._extract_recreation_codes9 %#udnn!=> s # u %rrc ||d|xsd}|Dcgc]r}t|ttfs|jt |nA|jt ||j t ||j zdzzfzt}}t ||_d}dg|_|D]B}||ddk7r |j n t|ddz }|jj|D|Dcgc]}|j|}}tjd|jzdj|z|j z|j"z}t%|d| |_nt%||_|,t( tj*d|jz|j&j,d|jg|jtj*dj|j&j,|j.g fd |Dtj*|j |j&j,|j0g fd |Dtj*|j"|j&j,|j2gyt ||_t5tj6|j|j&j,d |_ycc}wcc}w) NrmrrrsrpT)rIrJc3dK|]'}t|tr|jn|)ywrR)rrencode.0v_encs r z)ShareableList.__init__..Zs(Px!Jq#$6!((4.A=xs-0c3@K|]}|jywrR)rrs rrz)ShareableList.__init__..`s3(Q!((4.(rr)rrr_types_mappingtype _alignmentr _list_len_allocated_offsetsintappendrstructcalcsize_format_size_metainfojoin_format_packing_metainfo_format_back_transform_codesrshmr| pack_intor__offset_data_start_offset_packing_formats_offset_back_transform_codeslist unpack_from) rHsequenceritem_formatsoffsetfmt_recreation_codesrequested_sizers @rrPzShareableList.__init__*s <8/~2H%  %D &dS%L9##DJ/,,T$Z83t9+G!+KL< % !]DNF()cD #SW^$//S"XN''..v6 AI!@H--d3 !$__d000!"--.112N$DNKDH#D)DH  D   d000    ))     ! '' QxP     -- ,, 4(3     11 11 $  !YDN&*""..HHLL'D #}$!s A7K>/LcL|dk\r|n||jz}||jk\s|jdkr tdtjd|jj |j |dzzd}|jd}|jt}|S)z>Gets the packing format for a single value stored in the list.r Requested position out of range.8srrry) r IndexErrorrrrr_rrzr{r|)rHpositionrr fmt_as_strs r_get_packing_formatz!ShareableList._get_packing_formatss'1}8(T^^2K  &DNNQ,>?@ @     HHLL  ( (8a< 7    hhwZZ * rc||jk\s|jdkr tdtjd|jj |j |zd}|j|}|S)z9Gets the back transformation function for a single value.rrb)rrrrrr_r_back_transforms_mapping)rHrtransform_codetransform_functions r_get_back_transformz!ShareableList._get_back_transformsv  &DNNQ,>?@ @++  HHLL  - - 8    "::>J!!rc||jk\s|jdkr tdtjd|jj |j |dzz|jt|j|}tjd|jj |j|z|y)zvSets the packing format and back transformation code for a single value in the list at the specified position.rrrrrrN) rrrrrr_rrr|rr)rHrrrurs r!_set_packing_format_and_transformz/ShareableList._set_packing_format_and_transforms  &DNNQ,>?@ @  HHLL  ( (8a< 7   i (  66u=  HHLL  - - 8   rcF|dk\r|n||jz} |j|j|z}tj|j ||j j|\}|j|}||}|S#t$r tdwxYw)Nrzindex out of range) rrrrrrrr_rr)rHrrrback_transforms r __getitem__zShareableList.__getitem__s'1}8(T^^2K 3,,t/F/Fx/PPF%%((2 DQ11(; 1   312 2 3s AB B c|dk\r|n||jz} |j|}|j|z}|j|}t |t tfs|jt|}|}nw|j|dz|z }t |t r|jtn|}t||kDr td|ddk(r|}n|jt |fz}|j|||tj ||j"j$||y#t$r t dwxYw)Nrzassignment index out of rangerz(bytes/str item exceeds available storagerr)rrrrrrrrrrrr|rrrrrrr_) rHrru item_offsetrcurrent_format new_format encoded_valueallocated_lengths r __setitem__zShareableList.__setitem__sO'1}8(T^^2K >11(;K,,{:F!55h?N%#u.,,T%[9J!M#66x!|D{R !+5# 6#\\)4<= = >s /D//Ec\t|j|jjdfS)Nrrm)rrWrrrTs rrXzShareableList.__reduce__s t~~DHHMM:B>>rc^tjd|jjddS)Nrpr)rrrr_rTs r__len__zShareableList.__len__s$!!#txx||Q7::rcz|jjdt|d|jjdS)NrZz, name=r[)rWr\rrrrTs rr]zShareableList.__repr__s3..))*!DJOqQQrc^djfdtjDS)z=The struct packing format used by all currently stored items.rc3@K|]}j|ywrR)r)rirHs rrz'ShareableList.format..s! 1FAD $ $Q '1Fr)rrangerrTs`rformatzShareableList.formats,ww 16t~~1F   rc&d|jdzzS)z>The struct packing format used for the items' storage offsets.rprrrTs rrz#ShareableList._format_size_metainfosdnnq())rc d|jzS)z>The struct packing format used for the items' packing formats.rrrTs rrz&ShareableList._format_packing_metainfosdnn$$rc d|jzS)z>The struct packing format used for the items' back transforms.rrrTs rrz*ShareableList._format_back_transform_codessT^^##rc&|jdzdzS)Nr rrrrTs rrz ShareableList._offset_data_starts"a''rc:|j|jdzS)Nr)rrrTs rrz%ShareableList._offset_packing_formatss&&)@)@)DDDrc:|j|jdzzS)Nrr)rrrTs rrz*ShareableList._offset_back_transform_codess++dnnq.@@@rc,tfd|DS)zCL.count(value) -> integer -- return number of occurrences of value.c3(K|] }|k( ywrRrm)rentryrus rrz&ShareableList.count.. s4te5E>ts)sum)rHrus `rcountzShareableList.counts4t444rcVt|D]\}}||k(s |cSt|d)zpL.index(value) -> integer -- return first index of value. Raises ValueError if the value is not present.z not in this container) enumerater)rHrurrs rindexzShareableList.index s7 )OHe~ /y(>?@ @rrR)&r\rirjrkrfloatboolrrrWrrr staticmethodrrPrrrrrrXrr]rlrrrrrrrrr classmethodtypes GenericAlias__class_getitem__rmrrrrsI*& S s j U u  NJ  @ .     GdGR " ,"JB?;R  **%%$$(( EEAA5 A$E$6$67r)rk__all__ functoolsrr0r!r<rrrrr5r%r&rrO_CREATr$r r rrrr|rrmrrrs O -  77d?JJ **ryy BEBEJ  ]8]8r