# Pipe 管道 import os, time import multiprocessing as mp def job1(pipe): for i in range(100): pipe.send(i) time.sleep(0.1) print("***") return 0def job2(pipe): while True: print(pipe.recv()) return 0def main(): pipe = mp.Pipe() p1 = mp.Process(target = job1, args = (pipe[0], )) p2 = mp.Process(target = job2, args = (pipe[1], )) p1.start() p1.join() p2.start() p2.join() print("All done.")if __name__ == "__main__": main()
import os, time import multiprocessing as mp def job1(n, m): if n==0: print("***{}***{}***".format(n, m)) for i in range(n): time.sleep(0.1) print("***{}***{}***".format(i, m)) return 0def main(): n_cpu = mp.cpu_count() print(n_cpu) pool = mp.Pool() for i in range(n_cpu): pool.apply_async(func = job1, args = (i, i*10)) pool.close() pool.join() print("All done.")if __name__ == "__main__": main()
posted on 2018-12-11 13:51 阅读( ...) 评论( ...)