简体   繁体   中英

Send commands to subprocess.Popen() process

I need to pass commands to a process on subprocess.Popen() but when I do, it only works if I use stdin.close() afterwards. My code is below.

sprocess.stdin.write('/stop'.encode())
sprocess.stdin.flush()
sprocess.stdin.close()
sprocess.stdout.close()

Works, but that is because of stdin.close() and I need to be able to do this without closing the pipes.

How can I pass commands to the process without closing the pipes after?

Note that sprocess.stdin.write('/stop'.encode()) will not add a newline! Your subprocess is probably expecting a line of input, signified by either a newline or EOF. Closing stdin will send that EOF.

Try sending sprocess.stdin.write('/stop\\n'.encode()) instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM