我正在尝试运行一个命令,该命令将在控制台中打印输出并将一些输出发送到串行端口(USB0)。
基于两个地方的输出,我需要确定采用哪个路径并继续执行程序。我想通过pexpect实现此功能,因为我需要添加超时并查找字符串列表。
我的意图是为不同的pexpect实例并行运行pexpect.expect命令。
我试图为这种情况创建一个最小的测试用例。
Ex-1:这是我所期望的。
def run_cmd(a, host, rt):
b = a.expect([pexpect.EOF, pexpect.TIMEOUT, host])
rt[host] = b
if __name__ == '__main__':
jobs = []
rt = multiprocessing.Manager().dict()
host = 'a.b.c.d'
a = pexpect.spawn("ssh %s" % host)
p = multiprocessing.Process(target=run_cmd, args=(a, host, rt))
jobs.append(p)
p.start()
for proc in jobs:
proc.join()
pprint(rt.values())
Ex-2:此工具正在运行,我无法为每个呼叫启动衍生工具。
def run_cmd(host, rt):
a = pexpect.spawn("ssh %s" % host)
b = a.expect([pexpect.EOF, pexpect.TIMEOUT, host])
rt[host] = b
if __name__ == '__main__':
jobs = []
rt = multiprocessing.Manager().dict()
p = multiprocessing.Process(target=run_cmd, args=('a.b.c.d', rt))
jobs.append(p)
p.start()
for proc in jobs:
proc.join()
pprint(rt.values())
上面给出的Ex-1导致以下错误。
Process Process-2:
Traceback (most recent call last):
File "/proj/sival2/ctallapa/tools/apps/Anaconda3/5.3.0/lib/python3.7/site-packages/ptyprocess/ptyprocess.py", line 705, in isalive
pid, status = os.waitpid(self.pid, waitpid_options)
ChildProcessError: [Errno 10] No child processes
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/proj/sival2/ctallapa/tools/apps/Anaconda3/5.3.0/lib/python3.7/site-packages/pexpect/pty_spawn.py", line 23, in _wrap_ptyprocess_err
yield
File "/proj/sival2/ctallapa/tools/apps/Anaconda3/5.3.0/lib/python3.7/site-packages/pexpect/pty_spawn.py", line 682, in isalive
alive = ptyproc.isalive()
File "/proj/sival2/ctallapa/tools/apps/Anaconda3/5.3.0/lib/python3.7/site-packages/ptyprocess/ptyprocess.py", line 711, in isalive
'process. Did someone else call waitpid() ' +
ptyprocess.util.PtyProcessError: isalive() encountered condition where "terminated" is 0, but there was no child process. Did someone else call waitpid() on our process?