Python3子进程通信示例

2023-07-21Python开发问题
6

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

问题描述

我是子处理的新手.

我只需要一个在 parent.pychild.py 之间进行通信的非常简单的 win32 示例.从 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子进程通信示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10

按10分钟间隔对 pandas 数据帧进行分组
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)...
2024-08-22 Python开发问题
11