ealeon ealeon - 4 months ago 21
Python Question

How to receive pickle via subprocess.Popen

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

import pickle
import sys

to_return = {'a':1, 'b': 2}
pickle.dump(to_return, sys.stdout)


OUTPUT:

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

Answer

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.