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 21:37
下一篇 2016-11-05 22:38

相关推荐

  • heartbeatV1+nfs实现高可用httpd

        随着互联网技术的不断发展,Web应用也越来越普遍,Web服务器的无故障工作时间就显得尤重要,但由于各种各样的原因,一台服务器并不能保证永远不出问题的运行,此时就需要一种机制来实现多台服务器共同为相同的来务功能提供服务,以确保任意一台服务器宕机后,不会影响其所承载的业务的访问。   &nbsp…

    Linux干货 2015-06-26
  • N25-第六周作业

      复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#;               s/要查找的内容/替换为的内容/修饰符 %:全文,相当…

    Linux干货 2017-02-13
  • LVS之nat&dr

    Evernote Export 负载均衡集群设计时的要点:        (1)session保持            session sticky(ip hash)            sess…

    Linux干货 2016-12-01
  • 逻辑卷配置管理

    逻辑卷管理逻辑卷创建1、fdisk t 8e //修改分区类型 2、pvcreate  /dev/sd{a7,b} //创建物理卷pvs pvdisplay   //查看物理卷信息 3、vgcreate  vg0  /dev/sd{a7,b}  //创建卷组v…

    Linux干货 2016-08-30
  • find命令用法及示例

    文件查找 locate,find可是实现在文件系统上查找符合条件的文件 实现工具:locate,find locate有几下几个特性 1、依赖于事先构建好的索引库; 2、系统自动实现;(周期性任务) 3、手动更新数据库(updatedb) /var/lib/mlocate/mlocate.db 工作特性: 查找速度快; 模糊查找; 非实时查找 lo…

    Linux干货 2016-08-18
  • LVS详解及拓扑具体实现

    LVS:Linux Virtual Server Linux Cluster:集群,为解决某个特定问题将多台主机组织起来,满足同一个需求; 单台主机处理能力有限,包括cpu、IO、内存、带宽等资源,无法满足客户端请求;把用户的请求分散到多个不同的服务器,分摊压力; 集群的类型: LB:Load Balancing,负载均衡集群;负载均衡器,或调度器、分发器;…

    Linux干货 2016-10-28