Python3.6子进程Popen通信挂起。超时不起作用

时间:2020-02-13 07:15:48

标签: subprocess python-3.6

我编写ControlCaller模块来控制只能同时执行一个命令。 我使用threading.Lock()来做到这一点。但是,有时锁不会永远释放。它似乎有错误,但我不知道。

有两种主要方法。入口点是“通话”:

def _command_caller(self, cmd, timeout):
    with subprocess.Popen(cmd, universal_newlines=True,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE) as process:
        try:
            stdout, stderr = process.communicate(timeout=timeout)
        except Exception:
            process.kill()
            raise Exception("run command timeout")
        retcode = process.poll()
        if retcode:
            self.logger.error("run command failed. %s", str(stderr))
            raise Exception("run command failed")
        else:
            return stdout

def call(self, cmd, timeout=10):
    start_time = time.time()
    if self.caller_lock.acquire(timeout=timeout) is False:
        return {"returncode": -1,
                "output": "cannot acquire lock"}
    try:
        self.logger.debug("Run cmd: %s", " ".join(cmd))
        result = {}
        stdout = self._command_caller(cmd, timeout=timeout)
        self.except_cnt = 0
        result = {"returncode": 0,
                  "output": stdout.strip()}
    except Exception as e:
        self.logger.debug("Caller handle exception")
        result = {"returncode": -1,
                  "output": str(e)}
    finally:
        self.logger.debug("Release Lock after run cmd: %s", " ".join(cmd))
        self.logger.debug("Cost Time: %s", str(time.time() - start_time))
        self.caller_lock.release()
        return result

日志消息:

2020-02-13 03:42:36,966 [DEBUG] - interface.control_caller: Run cmd: ipmitool sdr
2020-02-13 03:42:38,055 [DEBUG] - interface.control_caller: Release Lock after run cmd: ipmitool sdr
2020-02-13 03:42:38,055 [DEBUG] - interface.control_caller: Cost Time: 1.0889253616333008
2020-02-13 03:42:38,056 [DEBUG] - interface.control_caller: Run cmd: ipmitool power status
2020-02-13 03:42:48,058 [WARNING] - interface.control_caller: cannot acquire lock
2020-02-13 03:43:01,062 [WARNING] - interface.control_caller: cannot acquire lock

0 个答案:

没有答案