Programing python 4th page 228
1 """ 2 IPC 3 http://www.cnblogs.com/BoyXiao/archive/2011/01/01/1923828.html 4 """ 5 import os,time 6 import threading 7 8 def child(pipeout): 9 zzz = 0 10 while True: 11 time.sleep(zzz) # make parent wait 12 msg=(‘spam %03d \n‘ % zzz).encode() # pipes are binary bytes 13 os.write(pipeout,msg) # send to parent 14 zzz = (zzz+1)%5 15 16 def parent(pipein): 17 while True: 18 line = os.read(pipein,32) # blocks until data sent 19 print(‘parent %d got [%s] at [%s]‘%(os.getpid(),line,time.ctime())) 20 21 pipein,pipeout = os.pipe() 22 threading.Thread(target = child,args=(pipeout,)).start() 23 parent(pipein)
时间: 2024-12-18 02:30:48