python-多进程

进程是由系统自己管理的。

1:最基本的写法

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    p = Pool(5)
    print(p.map(f, [1, 2, 3]))
[1, 4, 9]

2、实际上是通过os.fork的方法产生进程的

unix中,所有进程都是通过fork的方法产生的。

multiprocessing Process
os

info(title):
    title
    , __name__
    (os, ):  , os.getppid()
    , os.getpid()

f(name):
    info()
    , name

__name__ == :
    info()
    p = Process(=f, =(,))
    p.start()
    p.join()

3、线程共享内存

threading

run(info_list,n):
    info_list.append(n)
    info_list

__name__ == :
    info=[]
    i ():
        p=threading.Thread(=run,=[info,i])
        p.start()
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

进程不共享内存:

multiprocessing Process
run(info_list,n):
    info_list.append(n)
    info_list

__name__ == :
    info=[]
    i ():
        p=Process(=run,=[info,i])
        p.start()

[1]
[2]
[3]
[0]
[4]
[5]
[6]
[7]
[8]
[9]

若想共享内存,需使用multiprocessing模块中的Queue

multiprocessing Process, Queue
f(q,n):
    q.put([n,])

__name__ == :
    q=Queue()
    i ():
        p=Process(=f,=(q,i))
        p.start()
    :
        q.get()

4、锁:仅是对于屏幕的共享,因为进程是独立的,所以对于多进程没有用

multiprocessing Process, Lock
f(l, i):
    l.acquire()
    , i
    l.release()

__name__ == :
    lock = Lock()

    num ():
        Process(=f, =(lock, num)).start()

hello world 0
hello world 1
hello world 2
hello world 3
hello world 4
hello world 5
hello world 6
hello world 7
hello world 8
hello world 9

5、进程间内存共享:Value,Array

multiprocessing Process, Value, Array

f(n, a):
    n.value = i ((a)):
        a[i] = -a[i]

__name__ == :
    num = Value(, )
    arr = Array(, ())

    num.value
    arr[:]

    p = Process(=f, =(num, arr))
    p.start()
    p.join()

0.0
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3.1415927
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]

#manager共享方法,但速度慢

multiprocessing Process, Manager

f(d, l):
    d[] = d[] = d[] = l.reverse()

__name__ == :
    manager = Manager()

    d = manager.dict()
    l = manager.list(())

    p = Process(=f, =(d, l))
    p.start()
    p.join()

    d
    l
# print '-------------'这里只是另一种写法
# print pool.map(f,range(10))
{0.25: None, 1: '1', '2': 2}
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

#异步:这种写法用的不多

multiprocessing Pool
time
f(x):
    x*x
    time.sleep()
    x*x

__name__ == :
    pool=Pool(=)
    res_list=[]
    i ():
        res=pool.apply_async(f,[i])   res_list.append(res)

    r res_list:
        r.get(timeout=10) #超时时间

0
1
4
9
16
25
36
0
1
49
4
64
9
81
16
25
36
49
64
81

同步的就是apply

原创文章,作者:黑白子,如若转载,请注明出处:http://www.178linux.com/57605

(0)
黑白子黑白子
上一篇 2016-11-05
下一篇 2016-11-05

相关推荐

  • 于浩的第一篇随笔

    人生只有两件事,努力工作,享受生活!

    2018-03-26
  • Microsoft: Empowering the Digital World

    Microsoft is a global technology company known for shaping the modern digital experience. From its iconic Windows operating system to the versatile Microsoft Office suite, the comp…

    Linux干货 2024-05-19
  • 网络知识

    网络知识 一、网络初识 1.网络概念  在计算机领域中,网络是信息传输、接收、共享的虚拟平台,通过它把各个点、面、体的信息联系到一起,从而实现这些资源的共享。 2.分类 按覆盖范围分: 局域网LAN(作用范围一般为几米到几十公里)。 城域网MAN(界于WAN与LAN之间)。 广域网WAN(作用范围一般为几十到几千公里)。 按拓扑结构分类: 总线型 …

    Linux干货 2016-09-01
  • apache工作模式及虚拟主机的配置

    apache的工作模式 MPM 名词解释MPM   Multipath Process Module 多道处理模块。Linux中常使用prefork worker event三种MPM 即apache常用的三种工作模式。 prefork prefork为多进程模型,每个进程响应一个请求。其工作过程简单说来就是一个主进程:负责生成n个 子进程(子进程…

    Linux干货 2016-07-02
  • varnish详解

      第一章    http缓存的基础概念 1、程序运行时具有局部性特征 时间局部性缓存的数据往往被打有时间缀,具有定期失效的特征,过期后会从源服务器检验请求验证是否需要重新拉取数据,某数据被访问后,该数据往往会再次在短时间内被访问到。 空间局部性被访问数据的周边数据被访问的概率会比其它常规数据访问大很多,所以这些访问数…

    Linux干货 2016-11-15
  • 网络的冰山一角

        这周我们学习了网络管理,每一天进度特别快,上课认真听了,但是感觉也只能听懂一半。所以我决定把网络管理作为这周博客写一写,这样相当于又复习了一边,能加深印象。 一、用户应用程序对网络的影响     1、批处理应用程序:无需直接人工交互,宽带很重要,但是并非关键性因素。 &n…

    2017-09-04