You need to close stdin on the first program.
Some things to take notice of:
There are buffers between pipes (what subprocess.PIPE creates for you), which vary in size per platform and usage. Don't worry about this just yet though as it's not as relevant as:
In this case specifically, sort requires having the full input read before being able to sort (you cannot sort things if you don't know what they are yet).
Due to 2, it has it's own buffer that collects and waits for the file descriptor to be closed, signalling it's completion ;)
Edit: Here's the example I was making. I find it personally cleaner to use a pipe directly, since you can then build up your input separately before spawning a process:
In [2]: import os
...: import subprocess
...:
...: # A raw os level pipe, which consists of two file destriptors
...: # connected to each other, ala a "pipe".
...: # (This is what subprocess.PIPE sets up btw, hence it's name! ;)
...: read, write = os.pipe()
...:
...: # Write what you want to it. In python 2, remove the `b` since all `str`ings are `byte` strings there.
...: os.write(write, b"blahblahblah")
...:
...: # Close stdin to signal completion of input
...: os.close(write)
...:
...: # Spawn process using the pipe as stdin
...: p = subprocess.Popen(['cat'], stdin=read)
...:
blahblahblah
Also, make sure you p.wait() for the completion of the process, or you can end up in situations where you have not gotten the full result yet.