Subprocess replaces os.popen, os.system, os.spawn, popen2 and commands. A simple example for piping would be:
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]
You could also use a memory mapped file with the flag=MAP_SHARED for shared memory between processes.
multiprocessing abstracts both pipes and shared memory and provides a higher level interface. Taken from the Processing documentation:
from multiprocessing import Process, Pipe
def f(conn):
conn.send([42, None, 'hello'])
conn.close()
if __name__ == '__main__':
parent_conn, child_conn = Pipe()
p = Process(target=f, args=(child_conn,))
p.start()
print parent_conn.recv() # prints "[42, None, 'hello']"
p.join()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…