多進程
os.fork()(Linux)
#coding=utf-8
import os
print(os.getpid())#此處打印父進程的ID
pid = os.fork() #pid爲生成的子進程ID
print(pid)
if pid == 0:
print("I am child process (%s) and my parent is (%s)"
%(os.getpid(),os.getppid()))
else:
print("I (%s) just created a child process (%s)."
%(os.getpid(),pid))
fork()函數,它也屬於一個內建函數,並且只在Linux系統下存在。它非常特殊。普通的函數調用,調用一次,返回一次,但是fork()調用一次,返回兩次,因爲操作系統自動把當前進程(稱爲父進程)複製了一份(稱爲子進程),然後分別在父進程和子進程內返回。子進程永遠返回0,而父進程返回子進程的PID。這樣做的理由是,一個父進程可以fork()出很多子進程,所以,父進程要記下每個子進程的ID,而子進程只需要調用getppid()就可以拿到父進程的ID,子進程只需要調用os.getpid()函數可以獲取自己的進程號。
os.getpid()
返回當前進程的ID
os.getppid()
返回當前進程的父進程ID
multiprocessing.process
help(multiprocessing.Process)
__init__(self, group=None, target=None, name=None, args=(),kwargs={})
參數說明:
group:進程所屬組,基本不用。
target:表示調用對象,一般爲函數。
args:表示調用對象的位置參數元組。
name:進程別名。
kwargs:表示調用對象的字典。
創建進程
示例1:
#coding=utf-8
import multiprocessing
def do(n):
name = multiprocessing.current_process().name
print(name,"starting")
print("work", n)
if __name__ == "__main__":
p_list = []
for i in range(5):
p = multiprocessing.Process(target=do,args=(i,))#args參數傳入元素
p_list.append(p)
p.start()#啓動子進程
for p in p_list:
p.join()#join()等待當前子進程執行結束
print(p_list)
示例2:
#coding=utf-8
from multiprocessing import Process
import os
import time
def sleeper(name,seconds):
print("Process ID# %s" %(os.getpid()))
print("Parent Process ID#%s" %(os.getppid()))
print("%s will sleep for %s seconds" %(name,seconds))
time.sleep(seconds)
if __name__ == "__main__":
child_proc = Process(target=sleeper,args=("bob",5))
child_proc.start()
print("in parent process after child process start")
print("parent process about to join child process")
child_proc.join()
print("in parent process after child process join")
print("the parent's parent process:%s" %os.getppid())
多進程模板程序
#coding=utf-8
from multiprocessing import Process
import urllib.request
import time
def func1(url):
response = urllib.request.urlopen(url)
html = response.read()
print(html[:20])
time.sleep(1)
def func2(url):
response = urllib.request.urlopen(url)
html = response.read()
print(html[:20])
time.sleep(1)
if __name__ == "__main__":
p1 = Process(target=func1,args=("https://www.baidu.com",),name="gloryroad1")
p2 = Process(target=func2,args=("https://www.sogou.com",),name="hhq")
p1.start()
p2.start()
p1.join()
p2.join()
time.sleep(1)
print("Done!")
測試單進程和多進程程序執行的效率
#coding=utf-8
import multiprocessing
import time
def m1(x):
time.sleep(0.01)
return x * x
if __name__ == "__main__":
pool = multiprocessing.Pool(multiprocessing.cpu_count())
i_list = range(1000)
time1 = time.time()
#並行執行進程池中的進程,此處把列表的每個值傳給m1函數
pool.map(m1,i_list)
time2 = time.time()
print("time elapse: ",time2 - time1)
time1 = time.time()
list(map(m1,i_list))
time2 = time.time()
print("time elapse: ",time2 - time1)
多進程(一)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.