ealeon ealeon - 7 months ago 48
Python Question

How to receive pickle via subprocess.Popen


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


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'

What can I do to get pickle back from stdout?

Thank you


Use pickle.loads to load the pickle from a string. pickle.load is for loading from a stream.

Two unrelated remarks:

  • 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.

  • 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.