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.www58058.com/57605

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

相關推薦

  • 第三周作業

    1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。 答:who | cut -f 1 -d " " | sort -u 2、取出最后登錄到當前系統的用戶的相關信息。 答:who | tail -n 1 3、取出當前系統上被用戶當作其默認shell的最多的那個shell。 答:cut -f7 -d: …

    Linux干貨 2016-11-25
  • LInux 網絡及相關進程作業管理

    馬哥教育網絡班23期+第四周課堂練習 Linux 網絡及相關進程作業管理 一、概述 1.1 簡介: 進入Linux學習第四周,這一周講的知識點非常的多,也特別的碎,感覺這一周的內容掌握起來有很大的難度,主要是要記的命令很多,包括yum 的一些安裝的命令,配置yum 源,相關的網絡的命令和參數,還有就是進程管理和作業管理中的相關工具的使用,最后還講了bash腳…

    Linux干貨 2016-10-24
  • Linux用戶和組管理常用命令

    Linux用戶和組管理常用命令 1、useradd:創建用戶   useradd [options] LOGIN     -u UID: [UID_MIN, UID_MAX]指定uid,(默認500|1000開頭)定義在/etc/login.defs     -o 配合-u 選項, 不檢查…

    Linux干貨 2017-04-04
  • 程序包管理rpm

    Linux程序包管理      API:Application Program Interface      ABI:Application Binary Interface         Unix…

    Linux干貨 2016-08-23
  • grep命令及正則表達式

    grep命令和正則表達式 grep基本概念 grep:global search regular expression and print out the line. 作用:文本過濾器,用于文本搜索,用指定“模式”逐行匹配。 模式:由正則表達式字符及文本字符所編寫的過濾條件 正則表達式:由一類特殊字符和文本字符所編寫的模式,其有些字符不表示字符字面意義,而表…

    Linux干貨 2016-11-05
欧美性久久久久