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

相关推荐

  • 知乎上推荐的几款快速上手的UI框架

    1、Flat UI Flat UI是一套精美的扁平风格 UI 工具包,基于 Twitter Bootstrap 实现。地址:designmodo/Flat-UI · GitHub 2、BootMetro 基于 Twitter Bootstrap 框架构建,用于创建 Windows 8 的 Metro 风格的网站,灵感来自于 Metro UI CSS 。地址:…

    2015-03-17
  • keepalived+lvs 实现站点高可用

    lvs实现负载均衡
    keepalived 解决 director单点和realserver监控检测

    2017-12-13
  • 如何删除一个目录下的所有文件,但保留一个指定文件。附一些常用命令

    解答: 假设这个目录是/xx/,里面有file1,file2,file3..file10   十个文件 方法如下: find /date -type f ! -name “file10″|xargs rm -f 另外还有其他的方法比如:rsync命令和bush的 extglob功能等。在此不一一列举。 附常用命令: 文件和目…

    2017-07-15
  • 正则表达式

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其他用户都没有任何访问权限 [root@localhost ~]#  mkdir /home/tuser1======>创建/home/tuser1目录 [root@localhost ~]#   cp  -a  /etc/skel/   /hom…

    2017-10-10
  • 作业权限管理

    一、在/data/testdir里创建的新文件自动属于g1组,组g2的成员如:alice能对这些新文件有读写权限,组g3的成员如:tom只能对新文件有读权限,其它用户(不属于g1,g2,g3)不能访问这个文件夹。 首先创建三个组g1,g2,g3,题目要求在/data/testdir目录里创建的文件自动属于g1组那么首先要将这个目录的属组改为g1,然后通过更改…

    Linux干货 2016-08-03
  • linux-第一周

    inux命令:(type)                  1,集成在bash中的命令,内部命令。依赖于shell类型。                  2,在文件系统路径下有…

    Linux干货 2017-05-20