multiprocess-Process-Pool-Queue-2017-6-8

<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org3e98343">1. .os.fork() 子進(jìn)程接受返回值 0,父進(jìn)程接受返回值是子進(jìn)程的 pid</a></li>
<li><a href="#orge45d9ce">2. 多進(jìn)程每個(gè)變量各擁有一份,互不影響</a></li>
<li><a href="#orgcca67d6">3. 多次 fork()</a></li>
<li><a href="#orge18a284">4. multiprocessing 模塊</a></li>
<li><a href="#orgb98fd3a">5. Process 子類</a></li>
<li><a href="#org2c79c73">6. 進(jìn)程池 Pool</a></li>
<li><a href="#org795128d">7. Queue 的表現(xiàn)</a></li>
</ul>
</div>
</div>

<a id="org3e98343"></a>

.os.fork() 子進(jìn)程接受返回值 0,父進(jìn)程接受返回值是子進(jìn)程的 pid

import os
rpid = os.fork()
if rpid<0:
    print("failed to fork")
elif rpid==0:
    print("I am the son process (%s),my parent process is (%s)"%(os.getpid(),os.getpid()))
else:
    print("i am parent process (%s),my son process is (%s)"%(os.getpid(),rpid))
print("will be run by son and parent")

In [4]: %run test5.py
i am parent process (24829),my son process is (25083)
will be run by son and parent
I am the son process (25083),my parent process is (25083)
will be run by son and parent
這種情況在 ipython 下創(chuàng)建了兩個(gè)進(jìn)程搶輸入,沒救

<a id="orge45d9ce"></a>

多進(jìn)程每個(gè)變量各擁有一份,互不影響

import os
rpid = os.fork()

n1=5
if rpid<0:
    print("failed to fork")
elif rpid==0:
    g,n=1,2
    print("I am the son process (%s),my parent process is (%s)"%(os.getpid(),os.getpid()))
    print(locals())
    print(globals())
    n1=6
    print(locals())
    print(globals())
else:
    g,n=3,6
    time.sleep(4)
    print("father sleep 4s,let son pass first!")
    print("i am parent process (%s),my son process is (%s)"%(os.getpid(),rpid))
    print(locals())
    print(globals())
print("will be run by son and parent")

---------------------------------------------------–—結(jié)果
I am the son process (25938),my parent process is (25938)
I am the son process (25938),my parent process is (25938)
{'g': 1, 'builtins': <module 'builtin' (built-in)>, 'file': 'test5.py', 'package': None, 'rpid': 0, 'n1': 5, 'time': <module 'time' (built-in)>, 'name': $
{'g': 1, 'builtins': <module 'builtin' (built-in)>, 'file': 'test5.py', 'package': None, 'rpid': 0, 'n1': 5, 'time': <module 'time' (built-in)>, 'name': $
{'g': 1, 'builtins': <module 'builtin' (built-in)>, 'file': 'test5.py', 'package': None, 'rpid': 0, 'n1': 6, 'time': <module 'time' (built-in)>, 'name': $
{'g': 1, 'builtins': <module 'builtin' (built-in)>, 'file': 'test5.py', 'package': None, 'rpid': 0, 'n1': 6, 'time': <module 'time' (built-in)>, 'name': $
will be run by son and parent
father sleep 4s,let son pass first!
i am parent process (25937),my son process is (25938)
{'g': 3, 'builtins': <module 'builtin' (built-in)>, 'file': 'test5.py', 'package': None, 'rpid': 25938, 'n1': 5, 'time': <module 'time' (built-in)>, '_name$
{'g': 3, 'builtins': <module 'builtin' (built-in)>, 'file': 'test5.py', 'package': None, 'rpid': 25938, 'n1': 5, 'time': <module 'time' (built-in)>, '_name$
will be run by son and parent

<a id="orgcca67d6"></a>

多次 fork()

import os
import time
pid = os.fork()
if pid == 0:
    print("haha 1 %s"%(os.getpid()))
else:
    print("hehe 2 %s"%(os.getpid()))
pid = os.fork()
if pid==0:
    print("haha 3 %s %s"%(os.getpid(),os.getppid()))
else:
    print("haha 4 %s %s"%(os.getpid(),os.getppid()))
time.sleep(1)

---------------------------------------------------------–—結(jié)果
hehe 2 26877
haha 1 26878
haha 4 26877 25179
haha 4 26878 26877
haha 3 26880 26878
haha 3 26879 26877

2 與 1 分別執(zhí)行一次,接下來 26878 走一遍 4,并產(chǎn)生一個(gè)子進(jìn)程執(zhí)行 3.26877 執(zhí)行 4
并產(chǎn)生一個(gè)子進(jìn)程執(zhí)行 3。

<a id="orge18a284"></a>

multiprocessing 模塊

Process 的 target 不僅可以傳遞函數(shù),也可以傳遞綁定方法,也可以傳遞類

from  multiprocessing import Process
import os
from time import sleep
import types
def run_proc(jj,name,age,**kwargs):
    for i in range(10):
        print("son process running,name=%s,age=%di,pid=%d..."%(name,age,os.getpid()))
        print(kwargs)
        sleep(0.5)

class A:
    pass

A.run_proc=types.MethodType(run_proc,A)
a=A()
#a.run_proc(18,m=12,t=13)

if __name__=='__main__':
    print('parent process is %d'%os.getpid())
    p=Process(target=run_proc,args=(a,'hi',18,),kwargs={"m":20})
    print("son process will running")
    p.start()
    sleep(1)
    p.terminate()
    p.join()
    print("son process have end")

from  multiprocessing import Process
import os
from time import sleep
import types

class A:
    def __init__(self,name,age,**kwargs):
        for i in range(10):
            print("son process running,name=%s,age=%di,pid=%d..."%(name,age,os.getpid()))
            print(kwargs)
            sleep(0.5)
    pass


#a.run_proc(18,m=12,t=13)

if __name__=='__main__':
    print('parent process is %d'%os.getpid())
    p=Process(target=A,args=('hi',18),kwargs={"m":20})
    print("son process will running")
    p.start()
    sleep(1)
    p.terminate()
    p.join()
    print("son process have end") 

<a id="orgb98fd3a"></a>

Process 子類

from multiprocessing import Process
import time
import os

class Process_Class(Process):
    def __init__(self,interval):
        Process.__init__(self)
        self.interval = interval
    def run(self):
        print("son process (%s)start running,parent process is (%s)"%(os.getpid(),os.getppid()))
        t_start = time.time()
        time.sleep(self.interval)
        t_stop = time.time()
        print("(%s)end ..,elapsed(%0.2f)s"%(os.getpid(),t_stop-t_start))

if __name__=='__main__':
    t_start=time.time()
    print("present process is (%s)"%os.getpid())
    p1 = Process_Class(2)
    p1.start()
    p1.join()
    t_stop = time.time()
    print("(%s)end...,elapsed (%0.2f)"%(os.getpid(),t_stop-t_start))

present process is (11503)
son process (29364)start running,parent process is (11503)
(29364)end ..,elapsed(2.00)s
(11503)end…,elapsed (2.01)

<a id="org2c79c73"></a>

進(jìn)程池 Pool

from multiprocessing import Pool
import os,time,random

def worker(msg):
    t_start=time.time()
    print("%s start run,pid is %d"%(msg,os.getpid()))
    time.sleep(random.random()*2)
    t_stop = time.time()
    print(msg,"end run.elapsed time is %0.2f"%(t_stop-t_start))

po=Pool(3)
for i in range(0,10):
    po.apply_async(worker,(i,))

print("-----start------")
po.close()
po.join()
print("----end-----")

0 start run,pid is 29668
2 start run,pid is 29670
1 start run,pid is 29669
–—start-–—
2 end run.elapsed time is 0.64
3 start run,pid is 29670
3 end run.elapsed time is 0.31
4 start run,pid is 29670
4 end run.elapsed time is 0.14
5 start run,pid is 29670
0 end run.elapsed time is 1.16
6 start run,pid is 29668
5 end run.elapsed time is 0.73
7 start run,pid is 29670
1 end run.elapsed time is 1.92
8 start run,pid is 29669
6 end run.elapsed time is 0.96
9 start run,pid is 29668
7 end run.elapsed time is 0.92
8 end run.elapsed time is 1.82
9 end run.elapsed time is 1.73
-—end–—

<a id="org795128d"></a>

Queue 的表現(xiàn)

from multiprocessing import Queue
q=Queue(3) 
q.put("msg1")
q.put("msg2")
print(q.full())  #False
q.put("msg3")
print(q.full()) #True
try:
    q.put("msg4",True,2)
except:
    print("msg queue is full,now the num is:%s"%q.qsize())
try:
    q.put_nowait("msg4")
except:
    print("msg queue is full,the num is:%s"%q.qsize())
if not q.full():
    q.put_nowait("mag4")
if not q.empty():
    for i in range(q.qsize()):
        print(q.get_nowait()) 

False
True
msg queue is full,now the num is:3
msg queue is full,the num is:3
msg1
msg2
msg3

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 歷經(jīng)十一載,上南少林!今終上頂一覽?。?/div>
    那些年8713閱讀 295評論 0 0
  • 雞湯文學(xué)興起于20世紀(jì)上半葉,那個(gè)時(shí)候,經(jīng)濟(jì)不景氣、不平等、戰(zhàn)爭等惡魔正在磨滅人類追求美好生活的心靈,所以出...
    靜齋先生閱讀 697評論 1 2
  • 小木還是像個(gè)小姑娘一樣貓著腰,墊著步小跑著過來,臉頰紅撲撲的,一把拉開椅子,坐下,看著三山臉上五年前留下的疤,語氣...
    李小鹿兒閱讀 485評論 0 0
  • 文/蔥蔥 我的眼睛里有發(fā)燒的物件 我的夢境里有斷翅的蝴蝶 我的思想里有焦灼的螞蟻 我的身子里有驕傲的氫氣 日子被雨...
    蔥蔥_閱讀 488評論 31 30

友情鏈接更多精彩內(nèi)容