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

相关推荐

  • AOP面向方面编程

    1.引言         软件开发的目标是要对世界的部分元素或者信息流建立模型,实现软件系统的工程需要将系统分解成可以创建和管理的模块。于是出现了以系统模块化特性的面向对象程序设计技术。模块化的面向对象编程极度极地提高了软件系统的可读性、复用性和可扩展性。向对象方法的焦点在于选择对象作为模块的主要单元,并将对象与系统的…

    Linux干货 2015-04-07
  • net25 第15周作业

    1、总结sed和awk的详细用法; SED sed模式空间 默认不编辑源文件,仅对模式空间中的数据做处理:而后,处理结束后,将模式空间打印屏幕 sed [options]’address+command’ file… -n:静默模式,不再默认显示模式空间的内容 -i:直接修改原文件 -e script -e script 同时执行多个脚本 -f /pat…

    Linux干货 2017-05-15
  • corosync + pacemaker搭建高可用mysql

    一、实验图     二、环境准备  1)确保时间同步 [root@SQL1 ~]# crontab -e  */5 * * * * /usr/sbin/ntpdate 172.16.2.15 [root@SQL2 ~]#…

    Linux干货 2015-06-30
  • Linux基本命令总结

    一.目录相关的命令 mkdir rmdir tree cd ls pwd   二.文件相关命令 less cat more tac rev stat head tail cut grep cp mv rm touch rename   三.分析文本的工具 wc sort uniq cut    

    2017-09-10
  • OSI和TCP/IP

            我们生活中各方面都离不开网络,那网络究竟是什么东西呢?它是怎么做到让在不同地点的人无视空间距离完成通信的呢?首先我们来了解一下网络的基本概念。网络是由节点和连线构成,表示诸多对象及其相互联系。在数学上,网络是一种图,一般认为专指加权图。网络除了数学定义外,还有具体…

    2017-09-02
  • shell编程循环语法作业

    判断/var/目录下所有文件的类型 [root@www sh.log]# cat typefile.sh  #!/bin/bash #author:DYW #显示目录下文件类型 if [ $# -lt 1 ];then echo "Please&nb…

    Linux干货 2016-08-21