码迷,mamicode.com
首页 > 编程语言 > 详细

Python 多进程 multiprocessing 下篇

时间:2018-12-11 14:33:30      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:tip   targe   app   color   pre   pool   div   print   style   

# 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 0

def job2(pipe):
    while True:
        print(pipe.recv())
    return 0

def 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 0


def 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()

 

Python 多进程 multiprocessing 下篇

标签:tip   targe   app   color   pre   pool   div   print   style   

原文地址:https://www.cnblogs.com/tangjicheng/p/10101730.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!