如何通过subprocess.Popen接收泡菜 [英] How to receive pickle via subprocess.Popen
问题描述
getPickle.py
getPickle.py
import pickle
import subprocess
cmd = ['rsh', 'host1', 'sendPickle.py']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
results = pickle.load(stdout)
print results
sendPickle.py
sendPickle.py
import pickle
import sys
to_return = {'a':1, 'b': 2}
pickle.dump(to_return, sys.stdout)
输出:
File "getPickle" line 10, in <module>
results = pickle.load(stdout)
AttributeError: 'str' object has no attribute 'readline'
我该怎么办才能使酱汁从stdout回来?
What can I do to get pickle back from stdout?
谢谢
推荐答案
使用pickle.loads
从字符串中加载泡菜. pickle.load
用于从流中加载.
Use pickle.loads
to load the pickle from a string. pickle.load
is for loading from a stream.
两条无关的话:
-
如果您使用的是Python 2,您可能想使用
import cPickle as pickle
,因为C版本的速度快了许多,而且功能也一样强大.
if you are using Python 2, you probably want to
import cPickle as pickle
because the C version is many time faster and just as powerful.
除非您特别想支持较旧的Python版本,否则最好在转储端使用protocol=-1
来指定最新的Pickle协议,该协议比默认的最低/最旧版本更有效.
unless you specifically want to support older Python versions, it is a good idea to use protocol=-1
on the dump side in order specify the latest Pickle protocol, which is more efficient than the default lowest/oldest version.
这篇关于如何通过subprocess.Popen接收泡菜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!