python subprocess communicate_为什么python.subprocess在proc.communicate()之后挂起?

comunicate只运行一次,然后关闭管道,因此如果要向其发送多个命令,则需要在同一字符串中逐个发送。

下面是一个经过调查、尝试线程、子进程32、stdin.write、stdout.read等后对我有用的示例。此信息不在用于通信的正式python引用信息中:https://docs.python.org/2/library/subprocess.html

在任何情况下,这里是代码,简单,没有线程,没有子进程32,在linux和windows上工作。是的,您必须知道向另一个进程发送命令的次数,但通常您知道这一点。除此之外,您还可以添加threads、cwd、shell=True或任何您可能需要的内容,但这是最简单的情况:def do_commands(self, cmd, parms):

proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE )

# wait for the process to terminate

out, err = process.communicate(cmd_parms)

errcode = process.returncode

return errcode, out, err

因此,例如,如果要将多个回车(\n)发送到被调用的应用程序和中间的a参数(以交互方式提醒您),您可以这样调用它:cmd_parms = "\n\n\n\n\nparm\n\n"

errcode, out, err = do_commands(command, cmd_parms)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值