Python3子流程交流示例 [英] Python3 subprocess communicate example

查看:87
本文介绍了Python3子流程交流示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是子处理的新手.

我只需要一个非常简单的win32示例,即可在 parent.py child.py 之间进行communication().从parent.py发送到child.py的字符串,由child.py更改,然后从parent.py发送回parent.py以进行print().

我之所以发布此信息,是因为发现的示例最终要么不是win32要么没有使用只是让我感到困惑的孩子.

I just need a really simple win32 example of communicate() between a parent.py and child.py. A string sent from parent.py to child.py, altered by child.py and sent back to parent.py for print() from parent.py.

I'm posting this because examples I have found end up either not being win32 or not using a child which just confuses me.

感谢您的帮助.

推荐答案

根据您的要求,这是一个简单的示例.此示例为Python 3.x(2.x要求稍作修改).

Here is a simple example as per your requirements. This example is Python 3.x (slight modifications are required for 2.x).

import subprocess
import sys

s = "test"
p = subprocess.Popen([sys.executable, "child.py"],
                     stdin=subprocess.PIPE,
                     stdout=subprocess.PIPE)
out, _ = p.communicate(s.encode())
print(out.decode())

child.py

s = input()
s = s.upper()
print(s)

我在Mac OS X上编写并测试了此代码.这里没有特定于平台的代码,因此没有理由也无法在Win32上运行.

I wrote and tested this on Mac OS X. There is no platform-specific code here, so there is no reason why it won't work on Win32 also.

这篇关于Python3子流程交流示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆