Consider this at the windows commandline.
scriptA.py | scriptB.py
I want to send a dictionary object from scriptA.py to scriptB.py by pickle:ing it and sending it over a pipe. But I don’t know how to accomplish this.
I’ve read some posts about this subject here, but usually there’s answers along these line:
Popen( "scriptA.py"´, ..., and so on )
But I don’t actually know the name of “scriptA.py”. I just want to get hold of the ready pipe object and send/receive the databuffer.
I’ve tried sys.stdout/stdout, but I get file-descriptor errors and basically haven’t tried that track very far.
The process is simple:
scriptA.py:
- (1) Pickle/Serialize dictionary into stringbuffer
- (2) Send stringbuffer over pipe
scriptB.py
- (3) Receive stringbuffer from pipe
- (4) Unpickle/Deserialize stringbuffer into dictionary
When you say this to a shell
The shell connects them with a pipe. You do NOTHING and it works perfectly.
Everything that
scriptA.pywrites to sys.stdout goes toscriptB.pyEverything that
scriptB.pyreads from sys.stdin came fromscriptA.pyThey’re already connected.
So, how do you pass a dictionary from stdout in A to stdin in B?
Pickle.
scriptA.pydumps the dictionary to stdout.scriptB.pyloads the dictionary from stdin.JSON.
scriptA.pydumps the dictionary to stdout.scriptB.pyloads the dictionary from stdin.This is already built-in to Python and takes very, very little code.
In scriptA,
json.dump( {}, sys.stdout )orpickle.dump( {}, sys.stdout )In scriptB,
json.load( sys.stdin )orpickle.load( sys.stdin )