[docs]classSubprocessFrontend(Frontend):"""A frontend that creates fresh subprocess at every call to communicate with the backend."""def__init__(self,root:Path,backend_paths:tuple[Path,...],backend_module:str,backend_obj:str|None,requires:tuple[Requirement,...],)->None:""" Create a subprocess frontend. :param root: the root path to the built project :param backend_paths: paths that are available on the python path for the backend :param backend_module: module where the backend is located :param backend_obj: object within the backend module identifying the backend :param requires: seed requirements for the backend """super().__init__(root,backend_paths,backend_module,backend_obj,requires,reuse_backend=False)self.executable=sys.executable@contextmanagerdef_send_msg(self,cmd:str,result_file:Path,msg:str)->Iterator[SubprocessCmdStatus]:# noqa: ARG002env=os.environ.copy()backend=os.pathsep.join(str(i)foriinself._backend_paths).strip()ifbackend:env["PYTHONPATH"]=backendprocess=Popen(args=[self.executable,*self.backend_args],stdout=PIPE,stderr=PIPE,stdin=PIPE,universal_newlines=True,cwd=self._root,env=env,)cast("IO[str]",process.stdin).write(f"{os.linesep}{msg}{os.linesep}")yieldSubprocessCmdStatus(process)
[docs]defsend_cmd(self,cmd:str,**kwargs:Any)->tuple[Any,str,str]:""" Send a command to the backend. :param cmd: the command to send :param kwargs: keyword arguments to the backend :return: a tuple of: backend response, standard output text, standard error text """returnself._send(cmd,**kwargs)