From 585133ab60b4870a4d23fcd1ccee97e46379b176 Mon Sep 17 00:00:00 2001 From: Mike Hiretsky Date: Fri, 22 Jun 2018 10:07:31 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BF=D0=B0=D1=80=D0=B0=D0=BC=D0=B5=D1=82=D1=80=20tim?= =?UTF-8?q?eout=20=D0=BD=D0=B0=20=D0=B2=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BE=D0=BC=D0=B0=D0=BD=D0=B4?= =?UTF-8?q?=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pym/calculate/lib/utils/files.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pym/calculate/lib/utils/files.py b/pym/calculate/lib/utils/files.py index d5ca057..ee7cbce 100644 --- a/pym/calculate/lib/utils/files.py +++ b/pym/calculate/lib/utils/files.py @@ -113,6 +113,8 @@ class PipeProcess(StdoutableProcess): def get_stdout(self): return PIPE +class ProcessTimeout(FilesError): + pass class process(StdoutableProcess): """Execute system command by Popen @@ -160,6 +162,7 @@ class process(StdoutableProcess): self.envdict = kwarg.get("envdict", os.environ.copy()) self.envdict["LANG"] = kwarg.get('lang', 'C') self.langc = "langc" in kwarg + self.timeout = kwarg.get("timeout", None) self.stderr = kwarg.get("stderr", PIPE) self.cwd = kwarg.get("cwd", None) @@ -226,7 +229,10 @@ class process(StdoutableProcess): _stderr = self.pipe.stderr.fileno() reads = [_stdout, _stderr] while True: - ret = select.select(reads, [], []) + ret = select.select(reads, [], [], self.timeout) + if not ret[0]: + self.kill() + raise ProcessTimeout() for fd in ret[0]: if fd == _stdout: s = self.pipe.stdout.readline() @@ -264,7 +270,11 @@ class process(StdoutableProcess): try: self._open() if self.cacheresult is None: - self.cacheresult, self.cacheerr = self.pipe.communicate() + if self.timeout is None: + self.cacheresult, self.cacheerr = self.pipe.communicate() + else: + for line in self.readByLine(): + pass return self.cacheresult except KeyboardInterrupt: self.kill()