<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org3e98343">1. .os.fork() 子进程接受返回值 0,父进程接受返回值是子进程的 pid</a></li>
<li><a href="#orge45d9ce">2. 多进程每个变量各拥有一份,互不影响</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. 进程池 Pool</a></li>
<li><a href="#org795128d">7. Queue 的表现</a></li>
</ul>
</div>
</div>
<a id="org3e98343"></a>
.os.fork() 子进程接受返回值 0,父进程接受返回值是子进程的 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 下创建了两个进程抢输入,没救
<a id="orge45d9ce"></a>
多进程每个变量各拥有一份,互不影响
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")
---------------------------------------------------–—结果
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)
---------------------------------------------------------–—结果
hehe 2 26877
haha 1 26878
haha 4 26877 25179
haha 4 26878 26877
haha 3 26880 26878
haha 3 26879 26877
2 与 1 分别执行一次,接下来 26878 走一遍 4,并产生一个子进程执行 3.26877 执行 4
并产生一个子进程执行 3。
<a id="orge18a284"></a>
multiprocessing 模块
Process 的 target 不仅可以传递函数,也可以传递绑定方法,也可以传递类
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>
进程池 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 的表现
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