【发布时间】:2016-11-30 18:28:22
【问题描述】:
我想通过管道一一发送命令来控制 Python 解释器,例如。 G。让 Python 解释器打印“hello”,等待 3 秒,然后让 Python 解释器打印“world”。
为什么在 bash 中出现以下代码:
{ echo 'from sys import stdout' ; echo 'stdout.write("hello\n")' ; echo 'stdout.flush()' ; sleep 3 ; echo 'stdout.write("world\n")' ; echo 'stdout.flush()' ; } | python3.5 -u
先等待 3 秒,然后打印:
hello
world
而下面的代码:
{ echo 'from sys import stdout' ; echo 'stdout.write("hello\n")' ; echo 'stdout.flush()' ; sleep 3 ; echo 'stdout.write("world\n")' ; echo 'stdout.flush()' ; } | while read -r l ; do echo $l ; done
第一次打印:
from sys import stdout
stdout.write("hello\n")
stdout.flush()
然后等待 3 秒,最后打印出来:
stdout.write("world\n")
stdout.flush()
?
最后,我想从第一个 Python 脚本控制第二个 Python 解释器,如上例所示(打印“hello”,等待 3 秒,打印“world”)。 “子流程” Python 模块可以做到这一点吗?
以下代码:
import logging
from select import select
from subprocess import Popen, PIPE, TimeoutExpired
from time import sleep
from threading import Thread
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s %(message)s")
logger = logging.getLogger()
def read_stdout(proc):
stdout_closed, stderr_closed = False, False
while True:
rlist, wlist, xlist = select([proc.stdout, proc.stderr], [], [], 1.2)
if len(rlist) == 0:
logger.debug("timeout")
continue
if proc.stdout in rlist:
new_data = proc.stdout.read(1024)
logger.debug("new_data on stdout: %r" % new_data)
if len(new_data) == 0:
stdout_closed = True
if proc.stderr in rlist:
new_data = proc.stderr.read(1024)
logger.debug("new_data on stderr: %r" % new_data)
if len(new_data) == 0:
stderr_closed = True
if stdout_closed and stderr_closed:
break
logger.debug("start")
proc = Popen(
#["bash", "-c", 'while read -r l ; do echo $l ; done'],
["python", "-u"],
stdin=PIPE,
stdout=PIPE,
stderr=PIPE,
bufsize=0)
thread = Thread(target=read_stdout, args=(proc,))
thread.start()
proc.stdin.write(b'from sys import stdout\n')
proc.stdin.write(b'stdout.write("hello\\n")\n')
proc.stdin.write(b'stdout.flush()\n')
proc.stdin.flush()
sleep(3)
proc.stdin.write(b'stdout.write("world\\n")\n')
proc.stdin.write(b'stdout.flush()\n')
proc.stdin.flush()
proc.stdin.close()
thread.join()
导致与上面的 bash 代码相同的问题:
当 Popen() 调用“python -u”时,日志输出为:
2016-07-27 00:46:05,208 start
2016-07-27 00:46:06,411 timeout
2016-07-27 00:46:07,612 timeout
2016-07-27 00:46:08,213 new_data on stdout: b'hello\nworld\n'
2016-07-27 00:46:08,216 new_data on stdout: b''
2016-07-27 00:46:08,216 new_data on stderr: b''
而当 Popen() 调用“bash -c 'while read -r l ; do echo $l ; done'”时,日志输出为:
2016-07-27 00:48:37,237 start
2016-07-27 00:48:37,239 new_data on stdout: b'from sys import stdout\n'
2016-07-27 00:48:37,239 new_data on stdout: b'stdout.write("hello\\n")\nstdout.flush()\n'
2016-07-27 00:48:38,440 timeout
2016-07-27 00:48:39,642 timeout
2016-07-27 00:48:40,242 new_data on stdout: b'stdout.write("world\\n")\n'
2016-07-27 00:48:40,242 new_data on stdout: b'stdout.flush()\n'
2016-07-27 00:48:40,242 new_data on stderr: b''
2016-07-27 00:48:40,242 new_data on stdout: b''
2016-07-27 00:48:40,242 new_data on stderr: b''
Python 解释器没有立即执行发送给它的命令的原因是什么?是否可以通过“子流程”模块以某种方式实现这一目标? (我假设“pexpect”会解决问题,但我想了解“子进程”是否或为什么不能解决它。)
【问题讨论】:
标签: pipe subprocess python-3.5