U e5dó0ã@sþddlZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z ddl m Z ddl mZddl mZddl mZd d d d gZd Ze d¡ZGdd„deƒZddd„Zdd„Zdd„Zdd„ZeƒZejZejZejZejZdS)éNé)Ú connection)Úprocess)Ú reduction)Úresource_tracker)Úspawn)ÚutilÚensure_runningÚget_inherited_fdsÚconnect_to_new_processÚset_forkserver_preloadéÚqc@sDeZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dS)Ú ForkServercCs.d|_d|_d|_d|_t ¡|_dg|_dS)NÚ__main__)Ú_forkserver_addressÚ_forkserver_alive_fdÚ_forkserver_pidÚ_inherited_fdsÚ threadingZLockÚ_lockÚ_preload_modules©Úself©rú2/usr/lib64/python3.8/multiprocessing/forkserver.pyÚ__init__"s  zForkServer.__init__c Cs|j| ¡W5QRXdS©N)rÚ_stop_unlockedrrrrÚ_stop*szForkServer._stopcCsV|jdkrdSt |j¡d|_t |jd¡d|_t |j¡sLt |j¡d|_dS)Nr) rÚosÚcloserÚwaitpidrÚis_abstract_socket_namespacerÚunlinkrrrrr/s    zForkServer._stop_unlockedcCs&tdd„|jDƒƒstdƒ‚||_dS)z>Set list of module names to try to load in forkserver process.css|]}t|ƒtkVqdSr)ÚtypeÚstr)Ú.0ÚmodrrrÚ @sz4ForkServer.set_forkserver_preload..z&module_names must be a list of stringsN)ÚallrÚ TypeError)rZ modules_namesrrrr >sz!ForkServer.set_forkserver_preloadcCs|jS)z”Return list of fds inherited from parent process. This returns None if the current process was not started by fork server. )rrrrrr DszForkServer.get_inherited_fdsc Csà| ¡t|ƒdtkr tdƒ‚t tj¡ª}| |j¡t  ¡\}}t  ¡\}}|||j t   ¡g}||7}zNz&t ||¡||fWW¢4W5QR£St  |¡t  |¡‚YnXW5t  |¡t  |¡XW5QRXdS)a;Request forkserver to create a child process. Returns a pair of fds (status_r, data_w). The calling process can read the child process's pid and (eventually) its returncode from status_r. The calling process should write to data_w the pickled preparation and process data. éz too many fdsN)r ÚlenÚMAXFDS_TO_SENDÚ ValueErrorÚsocketÚAF_UNIXZconnectrr ÚpiperrZgetfdr!rZsendfds)rÚfdsZclientZparent_rÚchild_wÚchild_rZparent_wZallfdsrrrr Ls(   ÿ     z!ForkServer.connect_to_new_processc s|j~t ¡|jdk r`t |jtj¡\}}|sBW5QR£dSt |j¡d|_ d|_d|_d}|j r”ddh‰t   d¡}‡fdd„|  ¡Dƒ}ni}t tj¡Ø}t d¡}| |¡t |¡sÐt |d ¡| ¡t ¡\}}ztzV| ¡|g} || ¡||j |f;}t  ¡} | gt ¡} | d |g7} t | | | ¡}Wnt |¡‚YnXW5t |¡X||_ ||_||_W5QRXW5QRXdS) zíMake sure that a fork server is running. This can be called from any process. Note that usually a child process will just reuse the forkserver started by its parent, so ensure_running() will do nothing. NzCfrom multiprocessing.forkserver import main; main(%d, %d, %r, **%r)Ú main_pathÚsys_pathÚignorecsi|]\}}|ˆkr||“qSrr)r'ÚxÚy©Z desired_keysrrÚ „sz-ForkServer.ensure_running..r1i€z-c)rrr rr r"ÚWNOHANGr!rrrrZget_preparation_dataÚitemsr0r1rZarbitrary_addressZbindrr#ÚchmodZlistenr2ÚfilenoZget_executableZ_args_from_interpreter_flagsZspawnv_passfds) rÚpidZstatusÚcmdÚdataÚlistenerZaddressÚalive_rZalive_wZ fds_to_passZexeÚargsrr;rr isN          ÿ    zForkServer.ensure_runningN) Ú__name__Ú __module__Ú __qualname__rrrr r r r rrrrr src Csà|rdd|kr8|dk r8dt ¡_zt |¡W5t ¡`X|D]&}z t|ƒWq<tk r`Yqk rÂ}z|j?t?j@kr²‚W5d}~XYnXqW5QRXW5QRXdS)zRun forkserver.rNTFcWsdSrr)Z_unusedrrrÚsigchld_handler¼szmain..sigchld_handlercSsi|]\}}|t ||¡“qSr)Úsignal)r'ÚsigÚvalrrrr<Æsÿzmain..)r@cSsg|]\}}|j‘qSr)Zfileobj)r'ÚkeyZeventsrrrÚ Úszmain..iéÿÿÿÿrzChild {0:n} status is {1:n}z.forkserver: waitpid returned unexpected pid %drzToo many ({0:n}) fds to send)ArZcurrent_processZ _inheritingrZimport_main_pathÚ __import__Ú ImportErrorrZ _close_stdinr r2Ú set_blockingrKÚSIGCHLDÚSIGINTÚSIG_IGNr>Ú set_wakeup_fdr0r1Ú selectorsZDefaultSelectorZ getsocknameÚ _forkserverrÚregisterZ EVENT_READZselectÚ SystemExitÚreadr"r=ÚChildProcessErrorÚpopÚ WIFSIGNALEDÚWTERMSIGÚ WIFEXITEDÚAssertionErrorÚformatÚ WEXITSTATUSÚ write_signedÚBrokenPipeErrorr!ÚwarningsÚwarnZacceptrZrecvfdsr.r-Ú RuntimeErrorÚforkÚ_exitÚextendÚvaluesÚ _serve_oneÚ ExceptionÚsysÚ excepthookÚexc_infoÚstderrÚflushÚOSErrorÚerrnoZ ECONNABORTED)Z listener_fdrEZpreloadr6r7ÚmodnameZsig_rZsig_wrJÚhandlersZ old_handlersZ pid_to_fdrDZselectorZrfdsrAÚstsr4Ú returncodeÚsr3r5ÚcodeÚ unused_fdsÚfdÚerrrÚmain§sÔ      üÿ ÿ          ÿÿ  ÿ  ÿÿ   þ r€c Csht d¡| ¡D]\}}t ||¡q|D]}t |¡q,|^t_tj_ t_ t  |¡}t   ||¡}|S)NrP)rKrWr>r r!rYrrZ_resource_trackerZ_fdrÚduprZ_main) r5r3r}rxrLrMr~Zparent_sentinelr|rrrrn1s  þ  rncCsNd}tj}t|ƒ|kr@t ||t|ƒ¡}|s6tdƒ‚||7}q t |¡dS)Nózunexpected EOFr)Ú SIGNED_STRUCTÚsizer-r r\ÚEOFErrorZunpack)r~rCZlengthr{rrrÚ read_signedHs  r†cCs<t |¡}|r8t ||¡}|dkr*tdƒ‚||d…}q dS)Nrzshould not get here)rƒZpackr Úwriteri)r~ÚnÚmsgÚnbytesrrrreRs   re)NN) rvr rXrKr0ZstructrprrgÚrrÚcontextrrrrÚ__all__r.ZStructrƒÚobjectrr€rnr†rerYr r r r rrrrÚs>      ÿ