Programing python 4th page 228

 """
IPC
http://www.cnblogs.com/BoyXiao/archive/2011/01/01/1923828.html
"""
import os,time
import threading def child(pipeout):
zzz = 0
while True:
time.sleep(zzz) # make parent wait
msg=('spam %03d \n' % zzz).encode() # pipes are binary bytes
os.write(pipeout,msg) # send to parent
zzz = (zzz+1)%5 def parent(pipein):
while True:
line = os.read(pipein,32) # blocks until data sent
print('parent %d got [%s] at [%s]'%(os.getpid(),line,time.ctime())) pipein,pipeout = os.pipe()
threading.Thread(target = child,args=(pipeout,)).start()
parent(pipein)

2.Bidirectional IPC

 """
location: programing python 4td page 229
spawn a child process/program,connect my stdin/stdput to child process's stdout/stdin
my reads and writes map to output and input streams of the spawn program;much like typing
together streams with subprocess module
"""
import os,sys
def spawn(prog,*args):
stdinFd = sys.stdin.fileno() #pass progname,cmdline args
stdoutFd =sys.stdin.fileno() # get desciptors for streams,normally stdin =0,stdout =1 parentStdin,childStdout = os.pipe() # make two IPC pipe changels
childStdin,parentStdout = os.pipe() # pipe returns(inputfd,outputfd)
pid = os.fork() #make a copy of this process
if pid:
os.close(childStdout) # in parent process after fork
os.close(childStdin) # close child ends in parent
os.dump2(parentStdin,stdinFd) # my sys.stdin copy = pipe1[0]
os.dump2(parentStdout,stdoutFd) # my sys.stdout copy = pipe2[1]
else:
os.close(parentStdin) # in child process afte fork:
os.close(parentStdout) # close parent ends in child
os.dump2(childStdin,stdinFd) # my sys.stdin copy = pipe2[0]
os.dump2(childStdout,stdoutFd) # my sysout copu = pipe1[1]
args = (prog,)+args
os.execvo(prog,args) # new program in this process
assert False,'execvp failed' # os.exec call never returns here if __name__=='__main__':
mypid = os.getpid()
spawn('python','pipes-testchild.py','spawm') # fork child program print('hello 1 from parent',mypid) # to child's stdin sys.stdout.flush() # subvert stdio buffering
reply = input() # from child's stdout
sys.stderr.write('parent got: "%s"\n' % reply) # stderr not tied to pipe print('hello 2 from parent',mypid)
sys.stdout.flush()
reply = sys.stdin.readline()
sys.stderr.write('parent got:"%s"\n' % reply[:-1])

3.Named Pipes(Fifos)pages 234

Create a long-lived pipe that exists as a real named file in the filesystem. such files are called named pipes(or sometime,fifos).

虽然其是任意程序之外的,但是和计算机中真实文件有关,不依赖被其他任务所共享的内存。因此可以用于线程,进程和独立程序的IPC机制。一旦命名管道文件创建,客户端可以通过名字打开并使用正常的文件操作进行读写。其是单向流。典型的操作是,服务器程序从fifos读数据,客户端程序写数据。另外,2个fifos集可以用于实现双向通信,和匿名通信所做的一样。fifos不支持远程网络连接。

 """
name pipes;os.mkfifo is not available on windown
thare is no reason to fork here ,since fifo file ipes
are external to proceses--shared fds in parent/child processes
are irrelevent;
"""
import os,time,sys
fifoname ='/tmp/pipefifo'
def child():
pipeout = os.open(filename,os.O_WRONLY) # open fifo pipe as fd
zzz = 0
while True:
time.sleep(zzz)
msg =('spam %03d\n' %zzz).encode()
os.write(pipeout,msg)
zzz =(zzz+1)%5
def parent():
pipein = open(fifoname,'r') # open file as text file object
while True:
line = pipein.readline()[:-1] #block until data sent
print('parent %d got "%s" at %s' % (os.getpid(),line,time.ctime())) if __name__ =='__main__':
if not os.path.exits(filename):
os.mkfifo(fifename) #create a named pipe file
if len(sys.argv) == 1:
parent() # run as parent if no args
else:
child() # else run as child process

最新文章

  1. iOS 7 tabbar 透明的问题
  2. 完美者的代言-ArrayList线程安全问题
  3. 20145205 《Java程序设计》第9周学习总结
  4. fatal error: Call to undefined function mysqli_connect()
  5. Redis系列二之事务及消息通知
  6. 【python】浅谈包
  7. vs-ps combination error
  8. Android listview的item设定高度
  9. 简单3d RPG游戏 之 005 选择敌人
  10. JQ绑定事件(1.9已经废除了live()等绑定事件方法,on()方法是官方推荐的绑定事件的一个方法)
  11. Android 学习手札(一) 应用程序架构
  12. python基础之字典、赋值补充
  13. Java web 入门知识 及HTTP协议详解
  14. Python基础-week01
  15. NOIP2017划水崩盘记
  16. SQL字符串处理!
  17. 用css解决table文字溢出控制td显示字数
  18. java结合testng,利用txt做数据源的数据驱动实例
  19. JS引用类型之Array
  20. Android分享内容和接收分享内容小小实现

热门文章

  1. [机器学习] ——KNN K-最邻近算法
  2. BZOJ4562: [Haoi2016]食物链
  3. 使用TFHelp解析Html
  4. STM32环境搭建/学习观点/自学方法 入门必看
  5. 规范和封装jdbc程序代码
  6. 【java学习笔记】字符串和Date的转换
  7. 【java基础学习】网络编程
  8. 后台系统组件:一丶bootstrap table
  9. JavaS:网页中的显示和隐藏
  10. hive --service metastore 出现的问题