views:

809

answers:

3

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
+5  A: 

When you say this to a shell

scriptA.py | scriptB.py

The shell connects them with a pipe. You do NOTHING and it works perfectly.

Everything that scriptA.py writes to sys.stdout goes to scriptB.py

Everything that scriptB.py reads from sys.stdin came from scriptA.py

They're already connected.

So, how do you pass a dictionary from stdout in A to stdin in B?

  1. Pickle. scriptA.py dumps the dictionary to stdout. scriptB.py loads the dictionary from stdin.

  2. JSON. scriptA.py dumps the dictionary to stdout. scriptB.py loads the dictionary from stdin.

This is already built-in to Python and takes very, very little code.

In scriptA, json.dump( {}, sys.stdout ) or pickle.dump( {}, sys.stdout )

In scriptB, json.load( sys.stdin ) or pickle.load( sys.stdin )

S.Lott
EXCEPT if he's on Windows and Python is being invoked by "file association" (a.py | b.py) instead of directly referencing the Python executable (python a.py | python b.y) or e.g. (\python26\python a.py | \python26\python) -- like he said, you get dark mutterings about "invalid file descriptor"; it's long-standing bug in Windows cmd.exe. Concidentally another questioner raised this issue only a few minutes ago.
John Machin
Sorry; forgot the URL: http://stackoverflow.com/questions/1057638/bad-pipe-filedescriptor-when-reading-from-stdin-in-python
John Machin
That was not someone else, it was me since I bumped into it when trying this solution. There's however an even older question about the file association problem which I redirected to instead.
sharkin
+1  A: 

The pipe just puts stdout of A to stdin of B.

A does:

import sys
sys.stdout.writelines(output)

B just does:

import sys
input = sys.stdin.readlines()
stefanw
A: 

When you are piping something, you are (generally) feeding the standard output of one program into the standard input of another. I think you should keep trying this path.

If you're having trouble just being able to read the output of your first script with your second, check out this question.

miknight