- 论坛徽章:
- 1
|
- #coding:utf-8
- import multiprocessing
- import time
- from multiprocessing import Process,freeze_support
- def proc1(pipe):
- i=0
- while True:
- i=i+1
- print "\nPROC1-SEND %s"%i
- pipe.send(i)
-
- time.sleep(3)
- print '\nPROC1-RECV:',pipe.recv()
- def proc2(pipe):
- while True:
- print '\n\t\t\tproc2 recv:',pipe.recv()
- time.sleep(3)
- print "\n\t\t\tproc2 send %c"% 65
- pipe.send('A')
- # Build a pipe
- pipe = multiprocessing.Pipe(duplex=True)
- #print pipe
- # Pass an end of the pipe to process 1
- p1 = multiprocessing.Process(target=proc1, args=(pipe[0],))
- # Pass the other end of the pipe to process 2
- p2 = multiprocessing.Process(target=proc2, args=(pipe[1],))
- if __name__ == '__main__':
- freeze_support()
- p1.start()
- p2.start()
- p1.join()
- p2.join()
复制代码 输出结果
PROC1-SEND 1
proc2 recv: 1
PROC1-RECV:
proc2 send A
proc2 recv:A
PROC1-SEND 2
2
PROC1-RECV:
proc2 send A
|
|