我需要在Python中的一个独立进程中运行一个交互式Bash实例,该实例有自己的专用TTY(我不能使用pexpect)。 我使用了在类似程序中常见的代码片段:
master, slave = pty.openpty()
p = subprocess.Popen(["/bin/bash", "-i"], stdin=slave, stdout=slave, stderr=slave)
os.close(slave)
x = os.read(master, 1026)
print x
subprocess.Popen.kill(p)
os.close(master)
但是当我运行它时,我会得到以下输出:
$ ./pty_try.py
bash: cannot set terminal process group (10790): Inappropriate ioctl for device
bash: no job control in this shell
Strace of the run显示一些错误:
...
readlink("/usr/bin/python2.7", 0x7ffc8db02510, 4096) = -1 EINVAL (Invalid argument)
...
ioctl(3, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, 0x7ffc8db03590) = -1 ENOTTY (Inappropriate ioctl for device)
...
readlink("./pty_try.py", 0x7ffc8db00610, 4096) = -1 EINVAL (Invalid argument)
代码片段看起来很简单,Bash没有得到它需要的东西吗?有什么问题吗?
这是在子流程中运行交互式命令的解决方案。它使用伪终端使stdout不阻塞(还有一些命令需要tty设备,例如bash)。它使用select处理输入并输出到子流程。
这是最后对我有效的解决方案(如qarma建议的那样):
相关问题 更多 >
编程相关推荐