本文實例講述了python執行子進程實現進程間通信的方法。分享給大家供大家參考。具體實現方法如下:
a.py:
1
2
3
4
5
6
7
|
import subprocess, time subproc = subprocess.Popen([ 'c:\python31\python.exe' , 'c:/b.py' ], stdin = subprocess.PIPE, shell = True ) time.sleep( 0.5 ) print ( 'start' ) subproc.stdin.write( 'data\n' ) subproc.communicate( 'data\n' ) print ( 'end' ) |
b.py:
1
2
3
4
|
import sys print ( 'receive...' ) s = sys.stdin.readline() print ( 'get:' , len (s), s) |
希望本文所述對大家的Python程序設計有所幫助。