41 lines
991 B
Python
41 lines
991 B
Python
#-*- encoding:utf-8 -*-
|
||
|
||
'''
|
||
@Author : dingjiawen
|
||
@Date : 2023/10/17 20:59
|
||
@Usage : 学习多进程库mutiProcessing的使用
|
||
@Desc :
|
||
'''
|
||
|
||
'''
|
||
参考:
|
||
[1] https://cuiqingcai.com/3335.html
|
||
[2] https://blog.csdn.net/qq_38120851/article/details/122504028
|
||
'''
|
||
|
||
from multiprocessing import Process
|
||
import time
|
||
|
||
|
||
class MyProcess(Process):
|
||
def __init__(self, loop):
|
||
Process.__init__(self)
|
||
self.loop = loop
|
||
|
||
def run(self):
|
||
for count in range(self.loop):
|
||
time.sleep(1)
|
||
print('Pid: ' + str(self.pid) + ' LoopCount: ' + str(count))
|
||
|
||
|
||
if __name__ == '__main__':
|
||
# 在这里介绍一个属性,叫做 deamon。每个线程都可以单独设置它的属性,如果设置为 True,当父进程结束后,子进程会自动被终止。
|
||
# 如果这里不join,只会打印Main process Ended!
|
||
for i in range(2, 10):
|
||
p = MyProcess(i)
|
||
p.daemon = True
|
||
p.start()
|
||
p.join()
|
||
|
||
|
||
print("Main process Ended!") |