# Coding: UTF-8
Import os
Import Time
from multiprocessing Import Process
DEF func1 ():
Print ( " 5func1 Start ... " )
Print ( " 6 sub-process of process ID 1>: " , os.getpid ())
Print ( " 7 1 child process the parent process ID>: " , os.getppid ())
the time.sleep ( 2 )
Print ( " 11func1 End ... " )
DEF func2 ():
Print ( " 8func2 Start ..." )
Print ( " 9 sub-process process number 2>: " , os.getpid ())
Print ( " 10 2 child process the parent process ID>: " , os.getppid ())
the time.sleep ( 2 )
Print ( " 12func2 End ... " )
IF the __name__ == ' __main__ ' :
start_time_bin = the time.time ()
Print ( " . 1 starts the main process. " )
Print ( " 2 main process number>: " , OS.
getpid())
print( " 3 main parent of the process number (i.e., the process ID pycharm)>: " , os.getppid ())
P1 = Process (target = func1,)
P2 = Process (target = func2,)
p1.start ()
P2 .start ()
Print ( " . 4 ends the main process. " )
p1.join ()
p2.join ()
end_time_bin = the time.time ()
Print ( " execution time of a parallel program 13 (seconds): " , end_time_bin - start_time_bin ) the results:
# 1 master process to start.
# 2 primary process ID>: 3620
# 3 main process of the parent process ID (ie pycharm process ID)>: 7420
#4. The main process ends
# 5func1 Start ...
# 6 sub-process process ID> 1: 3088
# 7 child process parent process ID> 1: 3620
# 8func2 Start ...
# 9 sub-process process ID> 2: 6348
# 10 2 child process the parent process ID>: 3620
# 11func1 End ...
# 12func2 End ...
# execution time of 13 parallel programs (in seconds): 2.375
Through the above codes, it can be seen during program execution, a total of four processes.
pycharm process ID: 7420
Main process ID: 3620
Child process ID: 3088
Child process ID: 6348
Master and child processes in parallel.
# coding:utf-8
import os
import time
def func1():
print("4func1 start...")
print("5子进程1的进程号>:", os.getpid())
print("6子进程1的父进程号>:", os.getppid())
time.sleep(2)
print("7func1 end...")
def func2():
print("8func2 start...")
print("9子进程2的进程号>:", os.getpid())
print("10子进程2的父进程号>:", os.getppid())
time.sleep(2)
print("11func2 end...")
if __name__ == '__main__':
start_time_chuan = time.time()
print("1进程开始.")
print("2进程号>:", os.getpid())
print("3进程的父进程号(即pycharm的进程号)>:", os.getppid())
func1()
func2()
print("12主进程结束.")
end_time_chuan = time.time()
print("13串行程序的执行时间(秒数):", end_time_chuan - start_time_chuan)
执行结果:
# 1进程开始.
# 2进程号>: 4692
# 3进程的父进程号(即pycharm的进程号)>: 7420
# 4func1 start...
# 5子进程1的进程号>: 4692
# 6子进程1的父进程号>: 7420
# 7func1 end...
# 8func2 start...
# 9子进程2的进程号>: 4692
# 10子进程2的父进程号>: 7420
# 11func2 end...
# 12主进程结束.
# 13串行程序的执行时间(秒数): 4.015625
通过上面的代码,可以看出程序执行的过程中,一共有2个进程.
pycharm的进程号: 7420
进程号: 4692
主进程和子进程是串行的.
总结:
遇到有IO阻塞的情况,并行的执行时间短,串行的执行时间长.
接下来咱们看下没有IO(input/output输入输出)阻塞的情况哈.
# coding:utf-8
import os
import time
from multiprocessing import Process
def func1():
print("5func1 start...")
print("6子进程1的进程号>:", os.getpid())
print("7子进程1的父进程号>:", os.getppid())
print("8func1 end...")
def func2():
print("9func2 start...")
print("10子进程2的进程号>:", os.getpid())
print("11子进程2的父进程号>:", os.getppid())
print("12func2 end...")
if __name__ == '__main__':
start_time_bin = time.time()
print("1主进程开始.")
print("2主进程号>:", os.getpid())
print("3主进程的父进程号(即pycharm的进程号)>:", os.getppid())
p1 = Process(target=func1,)
p2 = Process(target=func2,)
p1.start()
p2.start()
print("4主进程结束.")
p1.join()
p2.join()
end_time_bin = time.time()
print("13并行程序的执行时间(秒数):", end_time_bin - start_time_bin)
# 1主进程开始.
# 2主进程号>: 7072
# 3主进程的父进程号(即pycharm的进程号)>: 7420
# 4主进程结束.
# 5func1 start...
# 6子进程1的进程号>: 8428
# 7子进程1的父进程号>: 7072
# 8func1 end...
# 9func2 start...
# 10子进程2的进程号>: 8204
# 11子进程2的父进程号>: 7072
# 12func2 end...
# 13并行程序的执行时间(秒数): 0.390625
通过上面的代码执行结果可以看到没有IO阻塞并行的情况,程序运行的时间是: 0.390625秒
接下来看下串行没有IO阻塞的运行时间哈
# coding:utf-8
import os
import time
def func1():
print("4func1 start...")
print("5子进程1的进程号>:", os.getpid())
print("6子进程1的父进程号>:", os.getppid())
print("7func1 end...")
def func2():
print("8func2 start...")
print("9子进程2的进程号>:", os.getpid())
print("10子进程2的父进程号>:", os.getppid())
print("11func2 end...")
if __name__ == '__main__':
start_time_chuan = time.time()
print("1进程开始.")
print("2进程号>:", os.getpid())
print("3进程的父进程号(即pycharm的进程号)>:", os.getppid())
func1()
func2()
print("12主进程结束.")
end_time_chuan = time.time()
print("13串行程序的执行时间(秒数):", end_time_chuan - start_time_chuan)
# 1进程开始.
# 2进程号>: 10040
# 3进程的父进程号(即pycharm的进程号)>: 7420
# 4func1 start...
# 5子进程1的进程号>: 10040
# 6子进程1的父进程号>: 7420
# 7func1 end...
# 8func2 start...
# 9子进程2的进程号>: 10040
# 10子进程2的父进程号>: 7420
# 11func2 end...
# 12主进程结束.
# 13串行程序的执行时间(秒数): 0.015625
哇呕,是不是发现了什么,没有IO阻塞的情况,串行的运行时间短,并行的运行时间长,因为并行比串行更占系统资源.