0

我需要可靠的 Python 代码来实现两个 Python 进程之间的通信。我编写了使用管道的此类代码。但我有些怀疑。有什么缺点?还有其他方法吗?

主要模块:

import subprocess, pickle
data=["A","B","C"]
s = pickle.dumps(data)
se=s.encode("string_escape") # produce a string that is suitable as string literal in Python source code
p=subprocess.Popen(["python", "server.py"],stdin = subprocess.PIPE, stdout= subprocess.PIPE, stderr= subprocess.PIPE)
stdout, stderr = p.communicate(input=se)
s2=stdout.decode("string_escape")
data = pickle.loads(s2)
print data

服务器.py:

import pickle,sys
s=sys.stdin.read().decode("string_escape")
data = pickle.loads(s)
s=pickle.dumps(data)
se=s.encode("string_escape")
sys.stdout.write(se)
4

0 回答 0