两个python进程之间通信的可靠代码

时间:2013-06-22 22:55:14

标签: python subprocess ipc pipe

我需要可靠的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

server.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)

0 个答案:

没有答案