How To Receive Pickle Via Subprocess.popen
Solution 1:
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.
Solution 2:
@user4815162342 gives the correct answer. To make it crystal clear, here's example code showing how to retrieve the pickled object.
Note that it's a bit of an unusual case to pair dump
and loads
, but that's what needs to be done as p.communicate
returns the string from stdout.
>>>import pickle>>>import subprocess as sp>>>cmd = ['python', 'sendPickle.py']>>>p = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE)>>>stdout, stderr = p.communicate()>>>stdout
"(dp0\nS'a'\np1\nI1\nsS'b'\np2\nI2\ns."
>>>results = pickle.loads(stdout)>>>results
{'a': 1, 'b': 2}
Post a Comment for "How To Receive Pickle Via Subprocess.popen"