我正在尝试在使用管道分叉的子进程和父进程之间进行通信。我能够在一个相当现代的python版本(下面的代码)中使用它。我很难在Jython 2.1中使用它(我没有选择更新它)。
有人能指出我正确的方向吗?
#!/usr/bin/env python
import os
import time
import sys
print "starting"
r, w = os.pipe()
prcID = os.fork()
if prcID:
# PARENT PROCESS
# Set up write pipe
os.close(r)
w = os.fdopen(w,'w')
for i in range(5):
time.sleep(1)
print "- " + str(i) + " -"
print "Parent is about to write"
w.write("OMG!!!! IT WORKS!!!!!")
print "Parent is done writing"
w.close()
os.waitpid(prcID, 0)
print "Parent done Waiting!"
else:
# CHILD PROCESS
os.close(w)
r = os.fdopen(r)
strout = r.read()
print "Child read: " + strout
print "Child done"
sys.exit(0)
#END of PARENT
print "done"