码迷,mamicode.com
首页 > 编程语言 > 详细

python并发编程02/多进程

时间:2019-08-21 00:34:42      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:操作系统   tle   ppi   间隔   优化   代码   结束   png   class   

python并发编程02/多进程

1.进程创建的两种方式

1.1开启进程的第一种方式

from multiProcessing import Process
import time
def task(name):
    print(f'{name} is running')
    time.sleep(2)
    print(f'{name} is gone')
if __name__ == '__main__':
    #在window环境下,开启进程必须__name=='__main__'下面
    p = Process(target=task,args=('太阳',)) #创建一个进程对象
    p.start() #只是向操作系统发出一个开辟子进程的信号,然后就执行下一行了
#这个信号操作系统接收到之后,会从内存中开辟子进程空间,然后再将主进程所有数据copy加载到子进程,然后再调用CPU去执行.
    #开辟子进程开销是很大的
    print('==主开始')
    time.sleep(3)
    #所以永远会先执行主进程的代码

1.2开启进程的第二种方式

# from multiprocessing import Process
# import time
#
# class MyProcess(Process):
#
#     def __init__(self,name):
#         super().__init__()
#         self.name = name
#
#     def run1(self):
#         print(f'{self.name} is running')
#         time.sleep(2)
#         print(f'{self.name} is gone')
#
#
# if __name__ == '__main__':
#     p = MyProcess('太阳')
#     p.start()
#     print('===主')

1.3简单应用

# from multiprocessing import Process
# import time
# 
# def task(name):
#     print(f'{name} is running')
#     time.sleep(1)
#     print(f'{name} is gone')
#
# def task1(name):
#     print(f'{name} is running')
#     time.sleep(2)
#     print(f'{name} is gone')
#
# def task2(name):
#     print(f'{name} is running')
#     time.sleep(3)
#     print(f'{name} is gone')
#
#
# if __name__ == '__main__':
#     # 在windows环境下, 开启进程必须在 __name__ == '__main__' 下面

#     # 一个进程串行的执行三个任务
#     start_time = time.time()
#     task('太阳')
#     task1('月亮')
#     task2('星星')
#     print(f'结束时间{time.time() - start_time}')
    # 三个进程 并发或者并行的执行三个任务
    # start_time = time.time()
    # p1 = Process(target=task, args=('太阳',))  # 创建一个进程对象
    # p2 = Process(target=task1, args=('月亮',))# 创建一个进程对象
    # p1.start()
    # p2.start()
    # task2('星星')
    # print(f'结束时间{time.time()-start_time}')

2.进程pid

2.1命令行获取所有的进程的pid tasklist

技术图片

2.2代码级别如何获取一个进程的pid

import os
print(os.getpid())

2.3获取父进程(主进程)的pid

技术图片

import os
import time
print(f'子进程:{os.getpid()}')
print(f'主(父)进程:{os.getppid(())}')

3.验证进程之间的空间隔离

#from multiprocessing import Process
#import os
#import time
# name = 'dabai'
#
# def task():
#     global name
#     name = 'ergou'
#     print(f'子进程{name}')
#
#
# if __name__ == '__main__':
#     p = Process(target=task)  # 创建一个进程对象
#     p.start()
#     # print('==主开始')
#     time.sleep(3)
#     print(f'主:{name}')

4.进程对象join方法

join让主进程等待子进程结束后,再执行主进程
# from multiprocessing import Process
# import time
#
# def task(name):
#     print(f'{name} is running')
#     time.sleep(2)
#     print(f'{name} is gone')
#
#
#
# if __name__ == '__main__':
#
#     p = Process(target=task,args=('太阳',))  # 创建一个进程对象
#     p.start()
#     p.join()
#     print('==主开始')

多个子进程使用join
# from multiprocessing import Process
# import time
#
# def task(name,sec):
#     print(f'{name}is running')
#     time.sleep(sec)
#     print(f'{name} is gone')
#
#
# if __name__ == '__main__':
#     start_time = time.time()
#     p1 = Process(target=task,args=('太阳',1))
#     p2 = Process(target=task,args=('月亮',2))
#     p3 = Process(target=task,args=('星星',3))
#     p1.start()
#     p2.start()
#     p3.start()
#     print(f'==主{time.time()-start_time}')  # 0.02 这只是主进程结束的时间,与其他进程毫无关系

验证 1
# from multiprocessing import Process
# import time
#
# def task(name,sec):
#     print(f'{name}is running')
#     time.sleep(sec)
#     print(f'{name} is gone')
#
#
# if __name__ == '__main__':
#     start_time = time.time()
#     p1 = Process(target=task,args=('太阳',1))
#     p2 = Process(target=task,args=('月亮',2))
#     p3 = Process(target=task,args=('星星',3))
#
#     p1.start()
#     p2.start()
#     p3.start()
#     # join 只针对主进程,如果join下面多次join 他是不阻塞的.
#     p1.join()
#     p2.join()
#     p3.join()
#
#     print(f'==主{time.time()-start_time}')

验证 2
# from multiprocessing import Process
# import time
#
# def task(name,sec):
#     print(f'{name}is running')
#     time.sleep(sec)
#     print(f'{name} is gone')
#
#
# if __name__ == '__main__':
#     start_time = time.time()
#     p1 = Process(target=task,args=('太阳',3))
#     p2 = Process(target=task,args=('月亮',2))
#     p3 = Process(target=task,args=('星星',1))
#
#     p1.start()
#     p2.start()
#     p3.start()
#     # join就是阻塞
#
#     p1.join()  # 等2s
#     print(f'==主1:{time.time()-start_time}')
#     p2.join()
#     print(f'===主2:{time.time()-start_time}')
#     p3.join()
#     print(f'==主3:{time.time()-start_time}')  #

优化上面的方法
# from multiprocessing import Process
# import time
# 
# def task(sec):
#     print(f'is running')
#     time.sleep(sec)
#     print(f' is gone')
# 
# 
# if __name__ == '__main__':
#     start_time = time.time()
    # p1 = Process(target=task,args=(1,))
    # p2 = Process(target=task,args=(2,))
    # p3 = Process(target=task,args=(3,))
    #
    # p1.start()
    # p2.start()
    # p3.start()
    # # join 只针对主进程,如果join下面多次join 他是不阻塞的.
    # p1.join()
    # p2.join()
    # p3.join()
    
    错误示范:
    # for i in range(1,4):
    #     p = Process(target=task,args=(i,))
    #     p.start()
    #     p.join()
    # '''
    # p1 = Process(target=task,args=(1,))
    # p1.start()
    # p1.join()
    # p2 = Process(target=task,args=(2,))
    # p2.start()
    # p2.join()
    # p3 = Process(target=task,args=(3,))
    # p3.start()
    # p3.join()
    #
    # '''
    
    正确示范:
    # l1 = []
    # for i in range(1, 4):
    #     p = Process(target=task,args=(i,))
    #     l1.append(p)
    #     p.start()
    #
    # for i in l1:
    #     i.join()
    #
    # print(f'==主{time.time()-start_time}')

join就是阻塞,主进程有join,主进程下面的代码一律不执行,直到进程执行完毕之后,在执行.

5.进程对象其他属性

# from multiprocessing import Process
# import time
#
# def task(name):
#     print(f'{name} is running')
#     time.sleep(2)
#     print(f'{name} is gone')
#
#
# if __name__ == '__main__':
#     # 在windows环境下, 开启进程必须在 __name__ == '__main__' 下面
#     # p = Process(target=task,args=('太阳',))  # 创建一个进程对象
#     p = Process(target=task,args=('太阳',),name='月亮')  # 创建一个进程对象
#     p.start()
#     # time.sleep(1)
#     # p.terminate()  # 杀死子进程  ***
#     # p.join()  # ***
#     # time.sleep(0.5)
#     # print(p.is_alive())   # ***
#     # print(p.name)
#     p.name = 'sb'
#     print(p.name)
#     print('==主开始')

6.守护进程

# 古时候 太监守护这个皇帝,如果皇帝驾崩了,太监直接也就死了.
子进程守护着主进程,只要主进程结束,子进程就跟着结束

# from multiprocessing import Process
# import time
#
# def task(name):
#     print(f'{name} is running')
#     time.sleep(2)
#     print(f'{name} is gone')
#
# if __name__ == '__main__':
#     # 在windows环境下, 开启进程必须在 __name__ == '__main__' 下面
#     p = Process(target=task,args=('太阳',))  # 创建一个进程对象
#     p.daemon = True  # 将p子进程设置成守护进程,只要主进程结束,守护进程马上结束.
#     p.start()
#     # p.daemon = True  # 一定要在子进程开启之前设置
#     time.sleep(1)
#     print('===主')

python并发编程02/多进程

标签:操作系统   tle   ppi   间隔   优化   代码   结束   png   class   

原文地址:https://www.cnblogs.com/liubing8/p/11386143.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!