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

相关推荐

  • CentOS 7上的性能监控工具

    Linux中基于命令行的性能监控工具:dstat、top、netstat、vmstat、htop、ss、glances 1、dstat – 多类型资源统计工具(需配置epel源)   该命令整合了vmstat,iostat和ifstat三种命令。同时增加了新的特性和功能可以让你能及时看到各种的资源使用情况,从而能够使你对比和整…

    Linux干货 2016-09-07
  • N26-第二周

    一、文件管理类命令 pwd:显示工作目录 cd:切换目录 cd [dir] cd: 切换回家目录;注意:bash中, ~表示家目录; cd ~:切换回自己的家目录 cd ~USERNAME:切换至指定用户的家目录; cd -:在上一次所在目录与当前目录之间来回切换; 相关的环境变量 $PWD:当前工作目录 $OLDPWD:上一次的工作目录 file:查看文件…

    Linux干货 2017-03-07
  • RAID简介

    RAID是什么? RAID英文名字叫Redundant Arrays of Independent Disks,也叫磁盘阵列?阵列有是什么意思?通俗的理解就是把很多的硬盘组织在一起来使用。       RAID可以根据性能来划分为两种:有无冗余(容错)能力     &n…

    Linux干货 2016-02-14
  • iptables详解

    iptables的工作机制 iptables有五个钩子函数(hook functions),也叫五个规则链。 1.PREROUTING (路由前) 2.INPUT (数据包流入口) 3.FORWARD (转发关卡) 4.OUTPUT(数据包出口) 5.POSTROUTING(路由后)   这是NetFilter规定的五个规则链,任何一个数据包,只要…

    Linux干货 2017-03-15
  • 马哥教育网络班22期-第2周博客作业1

    1、Linux上的文件管理类命令都有哪些?其常用的使用方法及其相关示例演示。    文件管理类命令:cp、mv、rm 1.1 cp命令    cp – copy files and directories    复制文件或目录 【SYNOPSIS】    单源复制:c…

    Linux干货 2016-08-22
  • LVS详解

    概述     LVS是工作在4层的负载均衡调度器,可根据请求报文的目标IP和目标协议及端口,根据指定的调度算法,将请求调度转发至某RealServer,本篇就针对LVS的原理,配置和使用进行简单介绍,具体包含:     1、LVS的四种类型的介绍   &nbs…

    Linux干货 2016-10-27