U :eD@sdZddgZddlmZddlZddlZddlZddlZddlZej dkrXddl Z dZ n ddl Z dZ ej ejBZd Ze r~d Znd Zd d ZGdddZdZGdddZdS)zProvides shared memory for direct access across processes. The API of this package is currently provisional. Refer to the documentation for details. SharedMemory ShareableList)partialNntFTz/psm_Zwnsm_cCsBtttd}|dks tdtt|}t|tks>t|S)z6Create a random filename for the shared memory object.z_SHM_NAME_PREFIX too long)_SHM_SAFE_NAME_LENGTHlen_SHM_NAME_PREFIXAssertionErrorsecretsZ token_hex)nbytesnamerB/usr/local/python38/lib/python3.8/multiprocessing/shared_memory.py_make_filename&s rc@seZdZdZdZdZdZdZej Z dZ e r.dndZ 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 Csl|dkstd|r0ttjB|_|dkr0td|dkrL|jtj@sLtdtrH|dkrt}ztj ||j|j d|_ Wnt k rYqZYnX||_ qqZn.|jrd|n|}tj ||j|j d|_ ||_ z<|r|rt|j |t|j }|j}t|j ||_Wn tk r*|YnXddlm}||j d n|r|dkr^tn|}ttjtjtj|d ?d @|d @|}zXt}|tjkr|dk rt t j!t"t j!|tjnWqNtjd ||d |_W5t|X||_ qVqNnX||_ t#tj$d|}zt%|tj$ddd} W5t|Xt&| }tjd ||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=True)mode/)register shared_memory lr)ZtagnameF)* ValueError_O_CREXosO_RDWR_flagsO_EXCL _USE_POSIXr _posixshmemZshm_open_mode_fdFileExistsError_name_prepend_leading_slash ftruncatefstatst_sizemmap_mmapOSErrorunlinkresource_trackerr_winapiZCreateFileMappingZINVALID_HANDLE_VALUEZNULLZPAGE_READWRITEZ CloseHandleZ GetLastErrorZERROR_ALREADY_EXISTSerrnoEEXISTstrerrorZOpenFileMappingZ FILE_MAP_READZ MapViewOfFileZVirtualQuerySize_size memoryview_buf) selfrcreatesizeZstatsrZ temp_nameZh_mapZlast_error_codeZp_bufrrr__init__Is             zSharedMemory.__init__cCs&z |Wntk r YnXdSN)closer+r5rrr__del__s zSharedMemory.__del__cCs|j|jd|jffS)NF) __class__rr7r;rrr __reduce__s zSharedMemory.__reduce__cCs|jjd|jd|jdS)N(z, size=))r=__name__rr7r;rrr__repr__szSharedMemory.__repr__cCs|jS)z4A memoryview of contents of the shared memory block.)r4r;rrrbufszSharedMemory.bufcCs.|j}tr*|jr*|jdr*|jdd}|S)z4Unique name that identifies the shared memory block.rrN)r$rr% startswith)r5Z reported_namerrrrs   zSharedMemory.namecCs|jS)zSize in bytes.)r2r;rrrr7szSharedMemory.sizecCsX|jdk r|jd|_|jdk r4|jd|_trT|jdkrTt|jd|_dS)zkCloses access to the shared memory from this instance but does not destroy the shared memory block.Nrr)r4releaser*r:rr"rr;rrrr:s     zSharedMemory.closecCs2tr.|jr.ddlm}t|j||jddS)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.r) unregisterrN)rr$r-rFr Z shm_unlink)r5rFrrrr,s   zSharedMemory.unlink)NFr)rA __module__ __qualname____doc__r$r"r*r4rrrr!rr%r8r<r>rBpropertyrCrr7r:r,rrrrr0s(  l     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!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|Sr9rvaluerrr zShareableList.cCs|dtSN)rstripdecode _encodingrOrrrrQ rRcCs |dSrS)rUrOrrrrQ rRcCsdSr9r)_valuerrrrQ rR)rrrcCs:t|ttdjfsdSt|tr$dSt|tr2dSdSdS)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.NrrrrY) isinstancestrbytesr=rOrrr_extract_recreation_codes  z&ShareableList._extract_recreation_codercs|dk rfdd|D}t|_tdd|Djks@ttfdd|D_fdd|D}tdjd |j j }nd }|dk r|dkrt |_ nt |d |d _ |dk rjttjdjj jd jfjtjd |j jjffd d|Dtjj j jjffdd|Dtjj j jjf|n t_tjj jd _dS)NcsPg|]H}t|ttfs$jt|n&jt|jt|jdfqS)r)rZr[r\_types_mappingtype _alignmentr .0itemr;rr s   z*ShareableList.__init__..css|]}t|dkVqdS)rNN)r rcfmtrrr )sz)ShareableList.__init__..c3s0|](}|ddkrjnt|ddVqdS)rsN)raintrfr;rrrh*scsg|]}|qSr)r]rbr;rrre.srLrNT)r6r7rc3s&|]}t|tr|n|VqdSr9)rZr[encodercv_encrrrhMsc3s|]}|VqdSr9)rlrmrorrrhSs)r _list_lensumr tuple_allocated_bytesstructcalcsize_format_size_metainfojoin_format_packing_metainfo_format_back_transform_codesrshmrW pack_intorC_offset_data_start_offset_packing_formats_offset_back_transform_codes unpack_from)r5ZsequencerZ_formatsZ_recreation_codesZrequested_sizer)rpr5rr8s|        zShareableList.__init__cCsj|dkr |n||j}||jks*|jdkr2tdtd|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) rq IndexErrorrurr{rCr~rUrVrW)r5positionrnrg fmt_as_strrrr_get_packing_formatds   z!ShareableList._get_packing_formatcCs\|dkr |n||j}||jks*|jdkr2tdtd|jj|j|d}|j|}|S)z9Gets the back transformation function for a single value.rrb)rqrrurr{rCr_back_transforms_mapping)r5rtransform_codeZtransform_functionrrr_get_back_transformts z!ShareableList._get_back_transformcCs~|dkr |n||j}||jks*|jdkr2tdtd|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) rqrrur|r{rCr~rlrWr]r)r5rrrPrrrr!_set_packing_format_and_transforms   z/ShareableList._set_packing_format_and_transformcCsjz6|jt|jd|}t|||jj|\}Wntk rRtdYnX| |}||}|S)Nzindex out of range) r}rrrtrurrr{rCrr)r5roffsetrnZback_transformrrr __getitem__s  zShareableList.__getitem__cCsz&|jt|jd|}||}Wntk rBtdYnXt|ttfsf|jt |}|}nZt|trz| t n|}t ||j|krt d|ddkr|}n|jt|j|f}||||t||jj||dS)Nzassignment index out of rangez(bytes/str item exceeds available storagerri)r}rrrtrrrZr[r\r_r`rlrWr rrrur|r{rC)r5rrPrZcurrent_formatZ new_formatZ encoded_valuerrr __setitem__s6  zShareableList.__setitem__cCst|j|jjddfS)Nr^r)rr=r{rr;rrrr>szShareableList.__reduce__cCstd|jjddS)NrLr)rurr{rCr;rrr__len__szShareableList.__len__cCs"|jjdt|d|jjdS)Nr?z, name=r@)r=rAlistr{rr;rrrrBszShareableList.__repr__csdfddtjDS)z>The struct packing format used by all currently stored values.rkc3s|]}|VqdSr9)r)rcir;rrrhsz'ShareableList.format..)rxrangerqr;rr;rformatszShareableList.formatcCs |jdS)z=The struct packing format used for metainfo on storage sizes.rLrqr;rrrrwsz#ShareableList._format_size_metainfocCs d|jS)z?The struct packing format used for the values' packing formats.rrr;rrrrysz&ShareableList._format_packing_metainfocCs d|jS)z?The struct packing format used for the values' back transforms.rrr;rrrrzsz*ShareableList._format_back_transform_codescCs|jddS)NrrNrr;rrrr}sz ShareableList._offset_data_startcCs|jt|jSr9)r}rrrtr;rrrr~sz%ShareableList._offset_packing_formatscCs|j|jdS)NrN)r~rqr;rrrrsz*ShareableList._offset_back_transform_codescstfdd|DS)zCL.count(value) -> integer -- return number of occurrences of value.c3s|]}|kVqdSr9r)rcentryrOrrrhsz&ShareableList.count..)rr)r5rPrrOrcountszShareableList.countcCs4t|D]\}}||kr|Sqt|ddS)zpL.index(value) -> integer -- return first index of value. Raises ValueError if the value is not present.z not in this containerN) enumerater)r5rPrrrrrindexs zShareableList.index)N)"rArGrHrIrjfloatboolr[r\r=r_rar staticmethodr]r8rrrrrr>rrBrJrrwryrzr}r~rrrrrrrrs^  F       )rI__all__ functoolsrr)rr/rur rr.rr O_CREATrrrr rrrWrrrrrs,    E