join用法
import multiprocessing
import os
import time
def info(title):
print(title)
time.sleep(2)
print(__name__)
print('father', os.getppid())
print('self', os.getpid())
print('--------')
if __name__ == "__main__":
p1 = multiprocessing.Process(target=info,args=('A1',))
p2 = multiprocessing.Process(target=info, args=('A2',))
p3 = multiprocessing.Process(target=info, args=('A3',))
p1.start()
p2.start()
p3.start()
p1.join()
p2.join()
p3.join()
print('all over')
'''
轮流执行
p1.start()
p1.join()
p2.start()
p2.join()
p3.start()
p3.join()
'''
```
import multiprocessing
import os
def func(conn):
print('func',os.getpid(), conn.recv())
conn.send(['a', 'b', 'c', 'd', 'e'])
conn.close()
if __name__ == "__main__":
conn_a, conn_b = multiprocessing.Pipe()
p = multiprocessing.Process(target=func, args=(conn_a,)).start()
conn_b.send([1, 2, 3, 4, 5, 6, 7])
print('mian',os.getpid(), conn_b.recv())
import multiprocessing
import os
queue = multiprocessing.Queue()
def func(myq):
print(os.getpid())
myq.put([1, 2, 3, 4])
if __name__ == '__main__':
print(os.getpid())
p = multiprocessing.Process(target=func, args=(queue,))
p.start()
print(queue.get())
import multiprocessing
import os
queue = multiprocessing.Queue()
def func(myq):
print(os.getpid())
myq.put([1, 2, 3, 4])
if __name__ == '__main__':
print(os.getpid())
p = multiprocessing.Process(target=func, args=(queue,))
p.start()
print(queue.get())
import multiprocessing
import os
queue = multiprocessing.Queue()
def func(myq):
print(os.getpid())
myq.put([1, 2, 3, 4])
if __name__ == '__main__':
print(os.getpid())
p = multiprocessing.Process(target=func, args=(queue,))
p.start()
print(queue.get())